spring事务系列(2)--声明式事务管理(传统)

来源:互联网 发布:达内 java西安 编辑:程序博客网 时间:2024/05/22 00:48

TransactionProxyFactoryBean声明式事务管理

上一节说了编程式事务管理,需要手动的更改service层的代码,下面说一下声明式事务的三种方式(事务环境和上一节是一样的,都是转账案例,只需要更改配置文件和测试代码)

service也做一下修改,不需要之前那么复杂

package com.yeshuai.spring.demo2;public class AccountServiceImpl implements AccountService {//注入转账的DAOprivate AccountDao accountDao;/** * @param out:转出账号 * @param in:转入账号 * @param money:转账金额 */@Overridepublic void transfer( String out, String in, Double money) {accountDao.outMoney(out, money);//int i = 1/0;accountDao.inMoney(in, money);}public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}}

声明式事务是基于aop的,那么需要引入传统aop开发的jar包



配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:task="http://www.springframework.org/schema/task"     xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"><!-- 引入外部的属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置业务层类 --><bean id="accountService" class="com.yeshuai.spring.demo2.AccountServiceImpl"><property name="accountDao" ref="accountDao" /></bean><!-- 配置DAO类(简化,会自动配置JdbcTemplate) --><bean id="accountDao" class="com.yeshuai.spring.demo2.AccountDaoImpl"><property name="dataSource" ref="dataSource" /></bean><!-- ==================================2.使用XML配置声明式的事务管理(原始方式)=============================================== --><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 配置业务层的代理 --><bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><!-- 配置目标对象 --><property name="target" ref="accountService" /><!-- 注入事务管理器 --><property name="transactionManager" ref="transactionManager"></property><!-- 注入事务的属性 --><property name="transactionAttributes"><props><!-- prop的格式:* PROPAGATION:事务的传播行为* ISOTATION:事务的隔离级别* readOnly:只读* -EXCEPTION:发生哪些异常回滚事务* +EXCEPTION :发生哪些异常不回滚事务 --><prop key="transfer">PROPAGATION_REQUIRED</prop><!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> --><!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> --></props></property></bean></beans>
传统的事务配置是对目标对象的一个增强(代理对象),在使用的时候,在TransactionTest中注入的是代理对象

package com.yeshuai.spring.demo2;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class TransactionTest {/** * 一定要注入代理类:因为代理类进行增强的操作 *///@Resource(name="accountService")@Resource(name="accountServiceProxy")private AccountService accountService;@Testpublic void demo1(){accountService.transfer("aaa", "bbb", 200d);}}




0 0
原创粉丝点击