本文介绍了@EnableTransactionManagement的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解在多个JavaConfig上下文的情况下放置 @EnableTransactionManagement 注释的正确位置在哪里?

I'm trying to understand where is the right place to put @EnableTransactionManagement annotation in case of multiple JavaConfig contexts?

考虑以下场景:我在JPAConfig.java中有JPA配置,在AppConfig.java中有一组服务bean。然后我在RootConfig.java中编写整体应用程序配置。

Consider following scenario: I have JPA config in JPAConfig.java and AppConfig.java with set of service beans. Then I compose overall application config in RootConfig.java.

我在JPAConfig.java中定义了事务管理器,并启用了对JPA存储库的扫描 - 因为那些暴露了事务行为,我把 @EnableTransactionManagement 在JPAConfig和它的作品。

I define transaction manager within JPAConfig.java as well as enable scanning for JPA repositories - as those expose transactional behavior, I put @EnableTransactionManagement over JPAConfig and it works.

然而,一些服务豆还需要有事务方法如在单个事务中访问多个存储库。我还应该在AppConfig上放置 @EnableTransactionManagement 吗?在我看来,这种注释的实现看起来会导致某些bean的重新定义。实际上这样做对我来说似乎没有用。

However, some of service beans also need to have transactional methods e.g. accessing several repositories within single transaction. Should I also put @EnableTransactionManagement over AppConfig as well? Looking into implementation of this annotation is seems to me that such approach would cause redefinition of some beans. And actually doing so doesn't seem to work for me.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("com.mypackage.repositories")
public class JPAConfig {
 // ... here are EntityManager and PlatformTransactionManager beans
}


@Configuration
@ComponentScan("com.mypackage.services")
// @EnableTransactionManagement // - ???
public class AppConfig {
}

@Configuration
@Import({AppConfig.class, JPAConfig.class})
public class RootConfig {
}

感谢任何建议。

推荐答案

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

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 eachpiece of context configuration although it does matter how early thisannotation is discovered as it registers internalTransactionAdvisorwhich actually processes @Transactional annotations on created beans.
  • In my case, I changed the order of contexts in @Import declaration sothat PersistenceConfig that holds @EnableTransactionManagement is thefirst. After this beans from other pieces can use AOP declarativetransaction.
  • 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!

这篇关于@EnableTransactionManagement的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:50
查看更多