spring boot 配置多个数据源,bean声明也使用了name区分,参数中也使用了@Qualifier进行区分,但任然报如下错误。
Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, but 2 were found:
- orderDataSource: defined by method 'orderDataSource' in class path resource [com/config/MultipleDBConfig.class]
- crmDataSource: defined by method 'crmDataSource' in class path resource [com/config/MultipleDBConfig.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
出现这个问题是由于spring boot默认配置的原因引起的,当有DataSource类型的bean出现时,DataSourceAutoConfiguration会去做一些相关的事情,所以在启动类上排除掉就可以了:
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
或
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
另外,你还可以参考如下文章所说:
Spring boot @Qualifier doesn't work with datasources
https://stackoverflow.com/questions/41415025/spring-boot-qualifier-doesnt-work-with-datasources