【问题标题】:Custom transaction managent in spring boot application with @EnableAutoConfiguration使用 @EnableAutoConfiguration 在 Spring Boot 应用程序中自定义事务管理
【发布时间】: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


    【解决方案1】:

    这实际上是 Spring Framework 4.1.4 中的一个错误。我们即将发布 4.1.5,但同时请降级到 4.1.3。

    SPR-12577

    【讨论】:

    • 感谢您的回答。如何为 spring boot 设置 spring 版本?我尝试在我的 pom.xml 中将 设置为 ,如下所述:[stackoverflow.com/questions/24402073/…,但该应用程序仍在使用版本 4.1.4 运行(来自日志文件“使用 Spring Boot v1. 2.1.RELEASE,Spring v4.1.4.RELEASE")
    • 只有在您使用 spring-boot-starter-parent 作为父母时才有效。您可能正在使用spring-boot-dependencies 范围内的import 集成引导自己?只需将spring-framework-bom 与范围import 以及引导之前添加。
    • 是的,使用此设置,我可以使用 spring 4.1.3 运行我的应用程序,但它会引发相同的异常。但是,我已经使用 @Primary 将 JpaTransactionManager bean 的显式创建添加到 CacheTransactionConfiguration 并且异常消失了。现在我对事务管理器本身有问题 - 它在 JUnit 测试(SpringJUnit4ClassRunner)中工作,但被应用程序忽略(不调用 cacheTransactionManager 方法),但这是单独线程的另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2019-06-12
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多