spring声明式事务管理,实现了AOP思想,提供TransactionInterceptor拦截器以及TransactionProxyFactoryBean代理类,对组件进行事务代理
首先 声明事务管理类
定义事务拦截器
也可以声明代理类ProxyFactoryBean
在spring jpetstore例子中,spring1.x和2.3中配置有所不同
1.x中:
这里,声明了一个抽象的BEAN,不可实例化,其他的声明事务的BEAN只要继承它,就会被AOP事务接管,配置了哪些方法被接管。
继承上面的baseTransactionProxy抽象bean,通过FactoryBean把事务对象(dataSource),与需要进行事务控制的对象PetStoreImpl串接起来,由AOP事务接管。这样一来,其实对PetStoreImpl具有侵入性。
在spring2中,petStore的bean被声明成
其实就是个一般的bean,简洁明了
而加入下面的声明:
这样,对抽象方法类
PetStoreImpl没有侵入,结构简单,所有的拦截关联和定义拦截处理的方法都在上面的代
码实现,非常清晰。
P.S. 上面配置文件各属性含义参考:
事务传播行为类型
PROPAGATION_REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选 择。
PROPAGATION_SUPPORTS
支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY
使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW
新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER
以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。(当使用PROPAGATION_NESTED时,底层的数据源必须基于JDBC 3.0 ,并且实现者需要支持保存点事务机制)
PROPAGATION_REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选 择。
PROPAGATION_SUPPORTS
支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY
使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW
新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER
以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。(当使用PROPAGATION_NESTED时,底层的数据源必须基于JDBC 3.0 ,并且实现者需要支持保存点事务机制)
首先 声明事务管理类
<
bean
id
="transactionManager"
class
="org.springframework.jdbc.datasource.
DataSourceTransactionManager" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
DataSourceTransactionManager" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
<
bean
id
= "transactionInterceptor"
class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="transactionAttributeSource" >
< value >
com.test.UserManager.*r=PROPAGATION_REQUIRED
</ value >
</ property >
</ bean >
class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="transactionAttributeSource" >
< value >
com.test.UserManager.*r=PROPAGATION_REQUIRED
</ value >
</ property >
</ bean >
也可以声明代理类ProxyFactoryBean
在spring jpetstore例子中,spring1.x和2.3中配置有所不同
1.x中:
<
bean
id
="baseTransactionProxy"
class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract ="true" >
< property name ="transactionManager" ref ="transactionManager" />
< property name ="transactionAttributes" >
< props >
< prop key ="insert*" > PROPAGATION_REQUIRED </ prop >
< prop key ="update*" > PROPAGATION_REQUIRED </ prop >
< prop key ="*" > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract ="true" >
< property name ="transactionManager" ref ="transactionManager" />
< property name ="transactionAttributes" >
< props >
< prop key ="insert*" > PROPAGATION_REQUIRED </ prop >
< prop key ="update*" > PROPAGATION_REQUIRED </ prop >
< prop key ="*" > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
<
bean
id
="petStore"
parent
="baseTransactionProxy"
>
< property name ="target" >
< bean class ="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl" >
< property name ="accountDao" ref ="accountDao" />
< property name ="categoryDao" ref ="categoryDao" />
< property name ="productDao" ref ="productDao" />
< property name ="itemDao" ref ="itemDao" />
< property name ="orderDao" ref ="orderDao" />
</ bean >
</ property >
</ bean >
< property name ="target" >
< bean class ="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl" >
< property name ="accountDao" ref ="accountDao" />
< property name ="categoryDao" ref ="categoryDao" />
< property name ="productDao" ref ="productDao" />
< property name ="itemDao" ref ="itemDao" />
< property name ="orderDao" ref ="orderDao" />
</ bean >
</ property >
</ bean >
在spring2中,petStore的bean被声明成
<
bean
id
="petStore"
class
="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl"
>
< property name ="accountDao" ref ="accountDao" />
< property name ="categoryDao" ref ="categoryDao" />
< property name ="productDao" ref ="productDao" />
< property name ="itemDao" ref ="itemDao" />
< property name ="orderDao" ref ="orderDao" />
</ bean >
< property name ="accountDao" ref ="accountDao" />
< property name ="categoryDao" ref ="categoryDao" />
< property name ="productDao" ref ="productDao" />
< property name ="itemDao" ref ="itemDao" />
< property name ="orderDao" ref ="orderDao" />
</ bean >
而加入下面的声明:
<
aop:config
>
<!--
关联所要引入事务的接口。pointcut属性定义PetStoreFacade下所有方法都会被拦截,
并以名字“txAdvice”指定的bean为处理“规则”,也就是定义哪些操作方法引入事务。
-->
< aop:advisor pointcut ="execution(* *..PetStoreFacade.*(..))" advice-ref ="txAdvice" />
</ aop:config >
<!--
定义交易行为,此处,默认事务行为是PROPAGATION_REQUIRED,所有以insert,update
开始的方法都会被处理,其他方法都被设定为readonly
-->
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
< tx:method name ="insert*" />
< tx:method name ="update*" />
< tx:method name ="*" read-only ="true" />
</ tx:attributes >
</ tx:advice >
<!--
关联所要引入事务的接口。pointcut属性定义PetStoreFacade下所有方法都会被拦截,
并以名字“txAdvice”指定的bean为处理“规则”,也就是定义哪些操作方法引入事务。
-->
< aop:advisor pointcut ="execution(* *..PetStoreFacade.*(..))" advice-ref ="txAdvice" />
</ aop:config >
<!--
定义交易行为,此处,默认事务行为是PROPAGATION_REQUIRED,所有以insert,update
开始的方法都会被处理,其他方法都被设定为readonly
-->
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
< tx:method name ="insert*" />
< tx:method name ="update*" />
< tx:method name ="*" read-only ="true" />
</ tx:attributes >
</ tx:advice >
码实现,非常清晰。
P.S. 上面配置文件各属性含义参考: