热爱技术,追求卓越
不断求索,精益求精

spring boot多数据库数据源启动报错“required a single bean, but 2 were found”的正确解决办法

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
赞(16)
未经允许不得转载:LoveCTO » spring boot多数据库数据源启动报错“required a single bean, but 2 were found”的正确解决办法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

热爱技术 追求卓越 精益求精