springboot mybatis調(diào)用多個(gè)數(shù)據(jù)源引發(fā)的錯(cuò)誤問(wèn)題
springboot mybatis調(diào)用多個(gè)數(shù)據(jù)源錯(cuò)誤
報(bào)錯(cuò)
'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [mssqlDataSource, postgreDataSource]
從后往前復(fù)制的,加粗的是重點(diǎn)。
因?yàn)橛卸鄠€(gè)數(shù)據(jù)源使用同一個(gè)mapper接口,但是都用@Primary,則會(huì)引起此錯(cuò)誤。
如圖所示:


從上面兩圖可以看出都用了同一個(gè)mapper接口,都添加了@Primary。
解決方法
解決方法有兩種,一種是把其中一個(gè)數(shù)據(jù)源去掉@Primary,動(dòng)態(tài)調(diào)用數(shù)據(jù)源,就是需要代碼切換使用的數(shù)據(jù)源。
如果要同時(shí)使用兩個(gè)數(shù)據(jù)源,那就用不同的mapper,相當(dāng)于postgre用postgre部分的mapper,sqlserver用sqlserver部分的mapper,大家互不干擾,就算@primary也沒(méi)事
如圖所示,我將postgre的MapperScan改了

springboot-mybatis多數(shù)據(jù)源及踩坑
springboot項(xiàng)目結(jié)構(gòu)如下

springboot配置文件內(nèi)容如下

動(dòng)態(tài)數(shù)據(jù)源的配置類(lèi)如下
(必須保證能被ComponentScan掃描到):
package com.letzgo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author allen
* @date 2019-01-10 15:08
*/
public class DynamicDatasourceConfig {
@Configuration
@MapperScan(basePackages = "com.letzgo.dao.master")
public static class Master {
@Primary
@Bean("masterDataSource")
@Qualifier("masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource dataSource() {
return new DruidDataSource();
}
@Primary
@Bean("masterSqlSessionFactory")
@Qualifier("masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
return factoryBean.getObject();
}
@Primary
@Bean("masterTransactionManager")
@Qualifier("masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("masterSqlSessionTemplate")
@Qualifier("masterSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.letzgo.dao.slave")
public static class Slave {
@Bean("slaveDataSource")
@Qualifier("slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean("slaveSqlSessionFactory")
@Qualifier("slaveSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml"));
return factoryBean.getObject();
}
@Bean("slaveTransactionManager")
@Qualifier("slaveTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("slaveSqlSessionTemplate")
@Qualifier("slaveSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
}完成基本配置之后,分別在master和slave中寫(xiě)一個(gè)數(shù)據(jù)庫(kù)訪問(wèn)操作,再開(kāi)放兩個(gè)簡(jiǎn)單的接口,分別觸發(fā)master和slave的數(shù)據(jù)看訪問(wèn)操作。
至此沒(méi)項(xiàng)目基本結(jié)構(gòu)搭建已完成,啟動(dòng)項(xiàng)目,進(jìn)行測(cè)試。
我們會(huì)發(fā)現(xiàn)這樣master的數(shù)據(jù)庫(kù)訪問(wèn)是能正常訪問(wèn)的,但是slave的數(shù)據(jù)庫(kù)操作是不行的,報(bào)錯(cuò)信息如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):***
對(duì)于這樣錯(cuò)誤,起初企圖通過(guò)百度解決,大部分都是說(shuō)xml文件的命名空間和dao接口全名不對(duì)應(yīng)或者說(shuō)是接口方法和xml中的方法不對(duì)應(yīng)等等解決方法,
本人檢查了自己的代碼多遍重啟多遍均無(wú)法解決,并不是說(shuō)這些方法不對(duì),但是本案例的問(wèn)題卻不是這些問(wèn)題導(dǎo)致的。最后無(wú)奈,只能硬著頭皮去看源碼,最后發(fā)現(xiàn)了問(wèn)題所在。
debug源碼調(diào)試到最后,發(fā)現(xiàn)不論是執(zhí)行mater還是slave的數(shù)據(jù)庫(kù)操作,使用了相同的SqlSession,同一個(gè)?。?!這個(gè)肯定是有問(wèn)題的。
繼續(xù)看源碼進(jìn)行查,看SqlSession的注入過(guò)程。
我們知道m(xù)ybatis只要寫(xiě)接口不用寫(xiě)實(shí)現(xiàn)類(lèi)(應(yīng)該是3.0之后的版本),實(shí)際上是使用了代理,每個(gè)dao接口,在spring容器中其實(shí)是對(duì)應(yīng)一個(gè)MapperFactoryBean(不懂FactoryBean的可以去多看看spring的一些核心接口,要想看懂spring源碼必須要知道的)。
當(dāng)從容器中獲取bean的時(shí)候,MapperFactoryBean的getObject方法就會(huì)根據(jù)SqlSession實(shí)例生產(chǎn)一個(gè)MapperProxy對(duì)象的代理類(lèi)。
問(wèn)題的關(guān)鍵就在于MapperFactoryBean,他繼承了SqlSessionDaoSupport類(lèi),他有一個(gè)屬性,就是SqlSession,而且剛才所說(shuō)的創(chuàng)建代理類(lèi)所依賴(lài)的SqlSession實(shí)例就是這個(gè)。那我們看這個(gè)SqlSession實(shí)例是什么時(shí)候注入的就可以了,就能找到為什么注入了同一個(gè)對(duì)象了。
找spring注入的地方,spring注入的方式個(gè)人目前知道的有注解處理器如@Autowired的注解處理器AutowiredAnnotationBeanPostProcessor等類(lèi)似的BeanPostProcessor接口的實(shí)現(xiàn)類(lèi),還有一種就是在BeanDefinition中定義器屬性的注入方式,在bean的定義階段就決定了的,前者如果不知道的可以看看,在此不做贅述,后者的處理過(guò)程源碼如下(只截取核心部分,感興趣的可以自己看一下處理過(guò)程,調(diào)用鏈比較深,貼代碼會(huì)比較多,看著眼花繚亂):

debug到dao接口類(lèi)的的BeanDefinition(上文已說(shuō)過(guò)其實(shí)是MapperFactoryBean),發(fā)現(xiàn)他的autowiremode是2,參照源碼

即可發(fā)現(xiàn)為按照類(lèi)型自動(dòng)裝配
最關(guān)鍵的來(lái)了
debug的時(shí)候發(fā)現(xiàn),master的dao接口執(zhí)行到this.autowireByType(beanName, mbd, bw, newPvs)方法中,給MapperFactoryBean中SqlSession屬性注入的實(shí)例是masterSqlSessionTemplate對(duì)象,
slave的dao接口執(zhí)行該方法時(shí)注入的也是masterSqlSessionTemplate對(duì)象,按類(lèi)型注入,spring容器中找到一個(gè)即注入(此時(shí)slaveSqlSessionTemplate也在容器中,為什么按類(lèi)型注入找到了masterSqlSessionTemplate卻沒(méi)報(bào)錯(cuò),應(yīng)該是@Primary的作用)
至此,問(wèn)題產(chǎn)生的原因已基本找到,那該如何解決呢?BeanDefinition為什么會(huì)定義成autowiremode=2呢,只能找@MapperScan看了,看這個(gè)注解的處理源碼,最后找到ClassPathMapperScanner以下方法:
private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
Iterator var3 = beanDefinitions.iterator();
while(var3.hasNext()) {
BeanDefinitionHolder holder = (BeanDefinitionHolder)var3.next();
GenericBeanDefinition definition = (GenericBeanDefinition)holder.getBeanDefinition();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface");
}
definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName());
definition.setBeanClass(this.mapperFactoryBean.getClass());
definition.getPropertyValues().add("addToConfig", this.addToConfig);
boolean explicitFactoryUsed = false;
if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionFactory != null) {
definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
explicitFactoryUsed = true;
}
if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
if (explicitFactoryUsed) {
this.logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionTemplate != null) {
if (explicitFactoryUsed) {
this.logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
explicitFactoryUsed = true;
}
if (!explicitFactoryUsed) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
}
definition.setAutowireMode(2);
}
}
}44行是關(guān)鍵,但是有個(gè)條件,這個(gè)條件成立的原因就是@MapperScan注解沒(méi)有指定過(guò)sqlSessionTemplateRef或者sqlSessionFactoryRef,正因?yàn)闆](méi)有指定特定的sqlSessionTemplate或者sqlSessionFactory,mybatis默認(rèn)采用按類(lèi)型自動(dòng)裝配的方式進(jìn)行注入。
至此,問(wèn)題解決方案已出:
代碼中的兩個(gè)@MapperScan用法分別改為:
@MapperScan(basePackages = "com.letzgo.dao.master", sqlSessionFactoryRef = "masterSqlSessionFactory", sqlSessionTemplateRef = "masterSqlSessionTemplate")?? @MapperScan(basePackages = "com.letzgo.dao.slave", sqlSessionFactoryRef = "slaveSqlSessionFactory", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
重啟進(jìn)行測(cè)試,問(wèn)題解決。
PS:
還是對(duì)各種注解使用方法不了解(或者說(shuō)對(duì)框架的源碼不了解),導(dǎo)致搞了這么久的問(wèn)題,還好最后查到了,記錄于此,給自己加深印象,以后還是要多看源碼。以上僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot如何通過(guò)Actuator顯示git和build的信息
這篇文章主要介紹了Spring?Boot通過(guò)Actuator顯示git和build的信息,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Java實(shí)現(xiàn)幾種序列化方式總結(jié)
本篇文章主要介紹了Java實(shí)現(xiàn)幾種序列化方式總結(jié),包括Java原生以流的方法進(jìn)行的序列化、Json序列化、FastJson序列化、Protobuff序列化,有興趣的可以了解一下,2017-03-03
Java中遍歷集合的并發(fā)修改異常解決方案實(shí)例代碼
當(dāng)你遍歷集合的同時(shí),又往集合中添加或者刪除元素,就可能報(bào)并發(fā)修改異常,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷集合的并發(fā)修改異常解決方案的相關(guān)資料,需要的朋友可以參考下2022-12-12
HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問(wèn)題
這篇文章主要介紹了HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

