springboot中的mybatis mulidatasource问题是“在候选者中发现了一个以上的“主” bean”

时间:2019-03-20 06:53:19

标签: spring mybatis

当我在springboot中配置mybatis多个数据源时,总是会出现“在候选者中发现多个”主bean”的问题。

主数据源的配置如下:

@Configuration
@MapperScan(basePackages = "com.monkey.myproject.dao.first", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DataSourceConfig1 {
    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.first")
    @Primary
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory1")
    @Primary
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource1) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource1);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/first/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManager1")
    @Primary
    public DataSourceTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource1) {
        return new DataSourceTransactionManager(dataSource1);
    }

    @Bean(name = "sqlSessionTemplate1")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory1)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory1);
    }
}

从属数据源的配置如下:

@Configuration
@MapperScan(basePackages = "com.monkey.myproject.dao.second", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DataSourceConfig2 {
    @Bean(name = "dataSource2")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource2) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource2);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/second/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManager2")
    public DataSourceTransactionManager transactionManager2(@Qualifier("dataSource2") DataSource dataSource2) {
        return new DataSourceTransactionManager(dataSource2);
    }

    @Bean(name = "sqlSessionTemplate2")
    public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory2)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory2);
    }
}

Junit测试如下:

@Autowired
@Qualifier("sqlSessionTemplate1")
private SqlSessionTemplate sqlSessionTemplate1;
@Test
public void test2() {
    try {
        sqlSessionTemplate1.insert("FirstMapper.insertOne", null);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

执行此测试时,出现异常消息,如下所示:

more than one 'primary' bean found among candidates: [primaryDataSource, secondaryDataSource, dataSource1, dataSource2]

注意:当我从master删除此注释'@Primary'时,此测试用例通过了。但这就是为什么?请帮助我,谢谢您。

1 个答案:

答案 0 :(得分:0)

此问题可能是由Spring Auto Configuration引起的,该问题几乎与每个Spring Starter都伴随。开发人员使用@Primary批注标记其bean有点奇怪。

因此您可以使用application.properties文件将其关闭:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

或带有以下注释:

@SpringBootApplication(exclude = [HibernateJpaAutoConfiguration::class])
or
@EnableAutoConfiguration(exclude = [HibernateJpaAutoConfiguration::class])