gpt4 book ai didi

java - 基于注释和基于 xml 的事务定义优先级

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:11:46 25 4
gpt4 key购买 nike

我在文档中找不到明确的答案,虽然似乎有一个合乎逻辑的答案,但不能确定。场景是这样的——您有一个基于 xml 的事务定义,例如:

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

它建议所有服务方法。但是你有@Transactional在具体的类/方法上,您想覆盖 propagation属性。

很明显@Transactional在方法级别覆盖类级别的相同方法,但它是否覆盖了 <tx:advice> (实际上, <aop:pointcut> )?

我希望不要在同一个类上创建两个拦截器,(无论哪个先创建都将启动事务)

最佳答案

感谢 skaffman 的努力。最后我想我得到了行为:

  1. 两者都是 <aop:advisor>@Transactional (与 <tx:annotation-driven> 一起)创建一个 TransactionInterceptor围绕目标类(一个,其方法将在事务中运行)。
  2. 低级建议order属性覆盖另一个。如果未指定顺序属性,则顺序未定义。但我的测试表明,在 applicationContext.xml 中定义的是最新的优先,尽管情况可能并非总是如此:

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.

至少这是 spring 2.5.6 的行为。

关于java - 基于注释和基于 xml 的事务定义优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com