spring tx:advice和spring aop pointcut之间的区别

7 java spring hibernate

我是春天的新手,具有hibernate的工作知识.我的工作是通过使用spring声明方法来实现事务.并且在Google的帮助下成功地做到了,感谢谷歌.但是无法清楚地理解我在application-context.xml中使用的术语.

1.

 <tx-advice>

</tx-advice>
Run Code Online (Sandbox Code Playgroud)
  1. <aop-config>
        // here is point cut were declared
    </aop-config>
    
    Run Code Online (Sandbox Code Playgroud)

可以有人解释我的上述观点,同时我也试图从谷歌中了解它.

sub*_*odh 28

正如您已成功实施的那样spring transaction,

Spring我们可以通过三种方式实现交易:

  1. 平台事务管理.
  2. 声明性交易管理.
  3. 程序化交易管理.

您实现的是通过XML称为声明式事务管理.

简而言之,你完成了transactionSpring的AOP功能的实现.

tx:advice XML配置与基于XML的AOP配置相结合可实现协同组合.例如,我们可以使用方法名称来自动确定我们想要在该方法上应用的事务类型.

假设我们想在与启动所有方法应用事务savemodifysavePizza(),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> 
Run Code Online (Sandbox Code Playgroud)

我们的建议已经准备就绪,正如我们上面所说的那样,我们只想在以save或开头的方法上进行交易modify.现在我们将通过使用pointcut元素来说明哪些bean需要上述建议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>
Run Code Online (Sandbox Code Playgroud)

简而言之,<tx:advice>意味着我们要做什么或者我们想要应用哪种交易行为. pointcut里面的元素<aop-config>说我们想要应用交易的地方,比方说<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>