【发布时间】:2015-04-17 04:04:29
【问题描述】:
我正在 Spring Boot 应用程序中为缓存内存(此时为简单的 HashMap)实现自定义事务管理。该应用程序已经使用了JpaTransactionManager,这是由@EnableAutoConfiguration 背后的一些魔法配置的。这是个问题,因为应用程序尝试加载两个PlatformTransactionManagers 并抛出:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: cacheTransactionManager,transactionManager.
事务管理器类:
@Component
public class KpiCacheTransactionManager extends AbstractPlatformTransactionManager{
...
}
我的事务管理器是由这个配置类加载的:
@Configuration
@EnableTransactionManagement
public class CacheTransactionConfiguration {
@Bean(name = "cacheTransactionManager")
public PlatformTransactionManager cacheTransactionManager() {
return new CacheTransactionManager();
}
}
主应用程序使用此配置运行:
@Configuration("MyApplication")
@EnableAutoConfiguration
@EntityScan("com.foo.bar")
@EnableJpaRepositories("com.foo.bar")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@ComponentScan("com.foo.bar")
@ImportResource({...})
public class MyApplication extends SpringBootServletInitializer{
}
我找到了一些可能的解决方案(@Primary 注释、经理名称、...),但我不知道如何在现有配置上使用@EnableAutoConfiguration 进行设置,如何覆盖 @ 的默认配置987654331@我自己的。
环境:Java 8、Spring Boot 1.2.1、Spring 4.1.4、Spring data JPA 1.7.2、Hibernate 4.3.7、Apache Tomcat 8.0.15
【问题讨论】:
标签: spring transactions spring-boot spring-data