基于注释和基于xml的事务定义优先级 [英] Annotation-based and xml-based transaction definitions precedence

查看:101
本文介绍了基于注释和基于xml的事务定义优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在文档中找到明确的答案,虽然似乎有一个合乎逻辑的答案,但人们无法确定。场景是这样的 - 你有一个基于xml的事务定义,如:

I couldn't find a definitive answer to this in the docs, and although there seems to be a logical answer, one can't be sure. The scenario is this - you have a xml-based transaction definition, like:

<tx:advice id="txAdvice" transaction-manager="jpaTransactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

建议使用所有服务方法。但是你在一个具体的类/方法上有 @Transactional ,你要覆盖传播属性。

Which advises all service methods. But then you have @Transactional on a concrete class/method, where you want to override the propagation attribute.

很明显,方法级别的 @Transactional 会覆盖同级别的同一个,但会覆盖< tx:advice> (实际上,< aop:pointcut> )?

It is clear that @Transactional at method-level overrides the same one at class-level, but does it override the <tx:advice> (and actually, the <aop:pointcut>)?

我希望不会在同一个类上创建两个拦截器(并且无论哪个首先都会启动该事务)

I hope two interceptors won't be created on the same class, (and whichever happens to be first will start the transaction)

推荐答案

感谢斯卡弗曼的努力。最后我觉得我有这样的行为:

Thanks to skaffman for his effort. Finally I think I got the behaviour:


  1. 两者< aop:advisor> @Transactional (连同< tx:annotation-driven> )创建 TransactionInterceptor 围绕目标类(其方法将在事务中运行)。

  2. 具有较低订单属性的建议会覆盖另一个。如果未指定order属性,则订单未定义。但我的测试表明, applicationContext.xml 中最新定义的那个优先,尽管可能并非总是如此:

  1. Both <aop:advisor> and @Transactional (together with <tx:annotation-driven>) create a TransactionInterceptor around the target class (the one, whose methods are to be run in transaction).
  2. The advice with a lower order attribute overrides the other one. If no order attribute is specified, the order is undefined. But my tests showed that the one defined latest in the applicationContext.xml takes precedence, although this might not be the case always:




当在不同方面定义的两条建议都需要在同一个连接点运行时,除非另外指定,否则执行顺序是未定义的。

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise the order of execution is undefined.

至少这是春季2.5.6的行为。

At least this is the behaviour for spring 2.5.6.

这篇关于基于注释和基于xml的事务定义优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆