java spring tx:advice 和 spring aop切入点的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12873570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between spring tx:advice and spring aop pointcut
提问by subodh
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. But not able to understand clearly about the terms I used in application-context.xml.
我是 spring 的新手,具有 hibernate 的工作知识。我的工作是使用 spring 声明式方法实现事务。感谢谷歌,我在谷歌的帮助下成功地做到了。但无法清楚地理解我在 application-context.xml 中使用的术语。
1.
1.
<tx-advice>
</tx-advice>
<aop-config> // here is point cut were declared </aop-config>
<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.
有人可以解释我上面的观点,同时我也试图从谷歌上理解它。
回答by subodh
As you already successfully implemented spring transaction
,
由于您已经成功实施spring transaction
,
In Spring
we can implement transaction in three ways:
在Spring
我们可以通过三种方式实现交易:
- Platform Transaction Management.
- DeclarativeTransaction Management.
- ProgrammaticTransaction Management.
What you implemented is called Declarative Transaction Management via XML.
您实现的称为通过 XML 的声明式事务管理。
In short you did the implementation of transaction
by Spring's AOPfeature.
简而言之,您完成了transaction
Spring 的AOP功能的实现。
Coupling the tx:advice XML
configuration with an XML based AOP configuration makes for synergistic combination. For example, we can use method names to automatically figure out what kind of transaction we want to apply on that method.
将tx:advice XML
配置与基于 XML 的 AOP 配置相结合,可以实现协同组合。例如,我们可以使用方法名称来自动确定我们要在该方法上应用哪种事务。
Saywe want to apply the transaction on all that methods which start with save
and modify
such as savePizza()
,saveColdDrink()
,modifyOrder()
,modifyBill()
. For these we have to define the advice
in our xml file:
假设我们想在与启动所有方法应用事务save
和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>
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
. Now we are going to say which beans require the above advice by using pointcut
element of aop-config
. 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.
我们的建议已经准备好了,正如我们通过使用上面的行所说的那样,我们只希望在以save
或开头的方法上进行交易modify
。现在我们将通过使用pointcut
元素来说明哪些 bean 需要上述建议aop-config
。例如,假设我们希望将事务建议应用于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.
pointcut
element inside <aop-config>
says where we want to apply the transaction,
say <aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
简而言之,<tx:advice>
意思是我们想要做什么或交易的哪种行为。
pointcut
里面的元素<aop-config>
表示我们想要在哪里应用事务,比如说<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
回答by rolve
The <tx:advice>
tag is specific for Transaction Managementconfiguration whereas the <aop:config>
tag can be used to do Aspect-Oriented Programmingin general.
该<tx:advice>
标签特定于事务管理配置,而该<aop:config>
标签通常可用于执行面向方面的编程。
AOPcan be used for many more things than transactions, for example logging or access control. 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可以用于比事务更多的事情,例如日志记录或访问控制。此外,事务管理不一定必须使用 AOP 来实现,它只是在 Spring 中通常完成的方式(但 Spring 也支持Programmatic Transaction Management)。