由于您已经成功实施spring transaction
,
In Spring
我们可以通过三种方式实现事务:
- 平台交易管理。
- 声明式 http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative交易管理。
- 程序化 http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html#transaction-programmatic交易管理。
你所实现的称为通过 XML 进行声明式事务管理.
简而言之,你执行了transaction
由春天的AOP http://en.wikipedia.org/wiki/Aspect-oriented_programming特征。
耦合tx:advice XML
配置与基于 XML 的 AOP 配置可以实现协同组合。例如,我们可以使用方法名称来自动确定我们想要在该方法上应用哪种类型的事务。
Say我们希望将事务应用于所有以以下开头的方法save
and modify
例如savePizza()
,saveColdDrink()
,modifyOrder()
,modifyBill()
。对于这些我们必须定义advice
在我们的 xml 文件中:
<tx:advice id="txAdvice" >
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
我们的建议已准备就绪,正如我们通过上面的行所说的那样,我们只希望在以以下开头的方法上进行交易save
or modify
。现在我们将通过使用来说明哪些 bean 需要上述建议pointcut
的元素aop-config
。例如,假设我们想要将事务建议应用于所有可用的类com.mytransaction.service
包裹。
为此,我们必须在 xml 文件中添加以下行:
<aop:config>
<aop:pointcut id="allServices"
expression="execution(*com.mytransaction.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
</aop:config>
简而言之,<tx:advice>
意味着要做什么或我们想要应用哪种交易行为。pointcut
里面的元素<aop-config>
表示我们要在哪里应用交易,
说<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>