数据库
首页 > 数据库> > springboot+mysql配置多数据源时遇到的问题

springboot+mysql配置多数据源时遇到的问题

作者:互联网

1.org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [clusterDataSource, masterDataSource]

解决方案:spring boot 启动类加上 exclude = DataSourceAutoConfiguration.class  代表启动项目的时候 不加载这个类

2.Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0

解决方案:Pom中的<relativePath /> 这一行需要被删掉

3.org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

报错原因,两个config文件中的方法上都加了@Primary

解决方案:将其中一个配置文件中的@Primary注掉就好了

4.more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

解决方案:因为项目中引用到第三方jar,第三方jar 里可以也有对ObjectMapper使用@Primary,造成冲突,去掉代码中的@Primary解决异常

5.Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found

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

出现原因:两个配置文件中的方法上都没有(或者都有)@Primary注解

解决方案:

  在springboot1.x时  datasource上要在一个写数据源上标记@Primary为主数据源

  而在springboot2.X时则不需要

     在其中一个配置文件中的方法上加上@Primary注解就好了

 

 

成功的方法:是在主masterConfig中的方法上加上@Primary

标签:springboot,found,数据源,Primary,bean,beans,mysql,org,than
来源: https://www.cnblogs.com/taosheng-yijiu/p/15096645.html