且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

@EnableTransactionManagement的范围是什么?

更新时间:2023-01-14 09:07:33

经过一些实验后,我似乎找到了自己的答案:

After some experiments I seem to have found the answer myself:


  • 没有必要每个
    片的上下文配置的配置 @EnableTransactionManagement 虽然它无论多么早它注册这个
    注释被发现 internalTransactionAdvisor
    实际处理创建的bean上的 @Transactional 注释。

  • 就我而言,我在 @Import 声明中更改了上下文的顺序,因此
    表示 PersistenceConfig 包含 @EnableTransactionManagement 首先是
    。在此之后,来自其他部分的bean可以使用AOP声明性
    交易。

  • 另一个警告涉及同时使用 @EnableTransactionManagement @EnableGlobalMethodSecurity 。全局方法安全性使用bean后处理,这似乎需要连接整个安全配置。 BeanPostProcessors是在上下文启动时尽早创建的,因此您不能在引导spring安全性所需的任何bean中使用声明性的 @Transactional (在我的情况下 UserDetailsContextMapper ) - 尚未创建顾问!

  • There is no need to configure @EnableTransactionManagement on each piece of context configuration although it does matter how early this annotation is discovered as it registers internalTransactionAdvisor which actually processes @Transactional annotations on created beans.
  • In my case, I changed the order of contexts in @Import declaration so that PersistenceConfig that holds @EnableTransactionManagement is the first. After this beans from other pieces can use AOP declarative transaction.
  • Another caveat relates to simultaneous use of @EnableTransactionManagement and @EnableGlobalMethodSecurity. Global method security uses bean post processing which seems to require whole security configuration to be wired. BeanPostProcessors are created early on context start-up so you can't use declarative @Transactional in any bean that would be needed to bootstrap spring security (in my case UserDetailsContextMapper) - advisor is not yet created then!