java - Spring Boot 和 Mybatis 项目中存在多个数据源时出现 NoUniqueBeanDefinitionException

标签 java spring-boot mybatis

在Spring Boot和Mybatis项目中配置多个数据源时,出现以下异常:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available: expected single matching bean but found 2: primaryTx,secondTx at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:384) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE] at com.sun.proxy.$Proxy86.findByDomain(Unknown Source) ~[na:na]

项目启动

@SpringBootApplication( exclude = {
        DataSourceAutoConfiguration.class, 
        DataSourceTransactionManagerAutoConfiguration.class
})
@EnableTransactionManagement
public class BookSystemApplication {
}

数据源配置

@Configuration
public class DataSourceConfig {
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

交易

@Configuration
public class TransactionConfig {
    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primary;

    @Autowired
    @Qualifier("secondDataSource")
    private DataSource second;

    @Bean(name="primaryTx")
    public PlatformTransactionManager primaryTransaction() {
        return new DataSourceTransactionManager(primary);
    }

    @Bean(name="secondTx")
    public PlatformTransactionManager secondTransaction() {
        return new DataSourceTransactionManager(second);
    }
}

最佳答案

这里的问题是您将两个 bean 定义为 datasource 并将两个 bean 定义为 TransactionManager 但您没有指定其中哪一个它们是 primary 一个,这不会起作用,因为 Spring 需要一个 datasource bean 和一个 TransactionManager 如果定义了多个 bean,则定义为主要 bean

您应该在这里做的是将数据源之一 beans 和 TransactionManager beans 之一定义为 Primary,以便 Spring可以正确运行,为此您需要使用@Primary注释

@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

请引用the Spring's Configure two datasources section来自文档。

关于java - Spring Boot 和 Mybatis 项目中存在多个数据源时出现 NoUniqueBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51306957/

相关文章:

java - 为什么我无法查询 SolrJ 的 URL?

java - 安卓java : Using a seekbar's progressValue correctly

java - 使用注解@SpringBootApplication 进行配置

java - Alter Session 在 MyBatis 中设置日期格式

java - Mybatis中的foreach是硬解析还是软解析?

java - 如何使用表(Spring)上的选择在MyBatis(Spring)映射器中创建对象?

java - Tomcat JNDI PSQLException : This connection has been closed

java - Maven Surefire 无法在 Jenkins 上实例化我的 IReporter 类

java - 使用@WebMvcTest 的 Spring Boot 测试在 Atlassian Bitbucket 管道上失败

java - 如何使用条件和分页