简体   繁体   English

Spring TX:建议和Spring AOP切入点之间的区别

[英]Difference between spring tx:advice and spring aop pointcut

I am new to spring, with working knowledge of hibernate. 我是新来的春天,具有冬眠的工作知识。 My job was to implement transaction by using spring declarative approach.And successfully i did with the help of Google, thanks to Google. 我的工作是通过使用Spring声明式方法来实现交易。由于Google的帮助,我成功地在Google的帮助下完成了交易。 But not able to understand clearly about the terms I used in application-context.xml. 但是无法清楚地了解我在application-context.xml中使用的术语。

1. 1。

 <tx-advice>

</tx-advice>
  1.  <aop-config> // here is point cut were declared </aop-config> 

can somebody explain me about above point, Meanwhile I am trying to understand it from the google also. 有人可以向我解释以上几点,与此同时,我也试图从google上了解它。

As you already successfully implemented spring transaction , 由于您已经成功实施了spring transaction

In Spring we can implement transaction in three ways: Spring我们可以通过三种方式实现事务:

  1. Platform Transaction Management. 平台交易管理。
  2. Declarative Transaction Management. 声明式事务管理。
  3. Programmatic Transaction Management. 程序化交易管理。

What you implemented is called Declarative Transaction Management via XML . 您实现的被称为通过XML的声明式事务管理

In short you did the implementation of transaction by Spring's AOP feature. 简而言之,您通过Spring的AOP功能实现了transaction的实现。

Coupling the tx:advice XML configuration with an XML based AOP configuration makes for synergistic combination. tx:advice XML配置与基于XML的AOP配置耦合在一起可以实现协同组合。 For example, we can use method names to automatically figure out what kind of transaction we want to apply on that method. 例如,我们可以使用方法名称来自动确定我们要对该方法应用哪种事务。

Say we want to apply the transaction on all that methods which start with save and modify such as savePizza() , saveColdDrink() , modifyOrder() , modifyBill() . ,我们要在其上用启动所有方法应用事务savemodify ,如savePizza() saveColdDrink() modifyOrder() modifyBill() For these we have to define the advice in our xml file: 对于这些,我们必须在xml文件中定义advice

<tx:advice id="txAdvice" >
  <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
  </tx:attributes>
</tx:advice> 

Our advice is ready, as we said by using above line that we want transactions only on the methods which start with save or modify . 正如我们在上面一行中所说的那样,我们的建议已经准备就绪,我们只希望以savemodify开头的方法进行事务处理。 Now we are going to say which beans require the above advice by using pointcut element of aop-config . 现在,我们要通过使用aop-config pointcut元素来说明哪些bean需要上述建议。 For example let say we want to apply the transaction advice to all of the classes which are available inside the com.mytransaction.service package. 例如,假设我们要将事务建议应用于com.mytransaction.service包中可用的所有类。

For this, we have to add the following line inside our xml file: 为此,我们必须在xml文件中添加以下行:

<aop:config>
  <aop:pointcut id="allServices"
    expression="execution(*com.mytransaction.service.*.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
</aop:config>

In-short, <tx:advice> mean what to do or which behavior of transaction we want to apply. 简而言之, <tx:advice>表示要执行的操作或我们想要应用的交易行为。 pointcut element inside <aop-config> says where we want to apply the transaction, say <aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/> <aop-config> pointcut元素表示要在其中应用事务的位置,例如<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>

The <tx:advice> tag is specific for Transaction Management configuration whereas the <aop:config> tag can be used to do Aspect-Oriented Programming in general. <tx:advice>标记特定于事务管理配置,而<aop:config>标记通常可用于进行面向方面的编程

AOP can be used for many more things than transactions, for example logging or access control. AOP可以用于事务之外的其他用途,例如日志记录或访问控制。 Also, transaction management does not necessarily have to be implemented using AOP, it's just the way it is usually done in Spring (but Spring also supports Programmatic Transaction Management ). 而且,不一定必须使用AOP来实现事务管理,这只是通常在Spring中完成的方式(但是Spring也支持Programmatic Transaction Management )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM