I'm trying to figure out where to place the @EnableTransactionManagement annotation in case of multiple JavaConfig contexts?
Consider the following scenario: I have a JPA config in JPAConfig.java and AppConfig.java with a set of beans services. Then I make up the general application configuration in RootConfig.java.
I define a transaction manager in JPAConfig.java and also allow scanning for JPA repositories - since those who are subject to transactional behavior, I put @EnableTransactionManagement on top of JPAConfig and it works.
However, some beans services must also have transactional methods, for example. Access to multiple repositories in a single transaction. Should I also put @EnableTransactionManagement on top of AppConfig? A look at the implementation of this annotation seems to me that such an approach can lead to an override of some beans. And actually it does not 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 { }
Appreciate any advice.
source share