使用@EnableAutoConfiguration在Spring启动应用程序中自定义事务管理

时间:2015-02-16 15:34:14

标签: spring transactions spring-boot spring-data

我在Spring启动应用程序中为缓存内存(此时简单HashMap)实现自定义事务管理。该应用程序已使用由JpaTransactionManager后面的某些魔法配置的@EnableAutoConfiguration。这是问题,因为应用程序尝试加载两个PlatformTransactionManager并抛出:

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在现有配置上设置此解决方案,如何使用我自己的覆盖JpaTransactionManager的默认配置。

环境:Java 8,Spring Boot 1.2.1,Spring 4.1.4,Spring数据JPA 1.7.2,Hibernate 4.3.7,Apache Tomcat 8.0.15

1 个答案:

答案 0 :(得分:1)

这实际上是Spring Framework 4.1.4中的一个错误。我们很快就会发布4.1.5,但与此同时请降级到4.1.3。

请参阅SPR-12577