spring boot+mybatis 多資料來源報錯 expected single matching bean but found
spring boot 多資料來源配置
No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: payment-dataSource,wacCommondataSource
之前一個數據源一切正常,今天突然增加一個數據源 如下兩個資料來源配置
配置的資料來源是沒有問題的,因為在另一個專案可以執行, 折騰了很久,最終通過跟蹤原始碼發現問題 原題在於專案依賴了 spring-boot-autoconfigure這個jar包<dependency><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
這個jar包中 有個DataSourceAutoConfiguration 會初始化DataSourceInitializer 這個類 ,這個類有一個init方法 會去獲取DataSource(資料來源)
@Configuration
@ConditionalOnClass({ DataSource.class,EmbeddedDatabaseType.class })
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ Registrar.class, DataSourcePoolMetadataProvidersConfiguration.class })
public class DataSourceAutoConfiguration {
@Configuration初始化方法中 會獲取資料來源 需要初始化一些ddl操作 也是就runSchemaScripts()方法 檢查初始化時是否需要執行sql script ,當你有兩個資料來源的時候,程式不知道取哪一個 ,所以報錯
@ConditionalOnMissingBean(DataSourceInitializer.class)
protected static classDataSourceInitializerConfiguration {
@Bean
public DataSourceInitializer dataSourceInitializer() {
return new DataSourceInitializer();
}
}
@PostConstruct解決辦法: 1.定義資料來源的地方 加個primary="true" 記得只給其中的一個加, 當多資料來源時 標示這個資料來源是主要的
public void init() {
if (!this.properties.isInitialize()) {
logger.debug("Initialization disabled (not running DDL scripts)");
return;
}
if (this.applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) {
this.dataSource = this.applicationContext.getBean(DataSource.class);
}
if (this.dataSource == null) {
logger.debug("No DataSource found so not initializing");
return;
}
runSchemaScripts();
}
private void runSchemaScripts() {
List<Resource> scripts = getScripts(this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) {
runScripts(scripts);
try {
this.applicationContext.publishEvent(new DataSourceInitializedEvent(
this.dataSource));
// The listener might not be registered yet, so don't rely on it.
if (!this.initialized) {
runDataScripts();
this.initialized = true;
}
}
catch (IllegalStateException ex) {
logger.warn("Could not send event to complete DataSource initialization ("
+ ex.getMessage() + ")");
}
}
}
<bean id="payment-dataSource" class="com.alibaba.druid.pool.DruidDataSource"2. spring boot 啟動類加上 exclude = DataSourceAutoConfiguration.class 代表啟動專案的時候 不載入這個類
init-method="init" destroy-method="close" primary="true">
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
相關推薦
spring boot+mybatis 多資料來源報錯 expected single matching bean but found
spring boot 多資料來源配置 No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: paymen
expected single matching bean but found 2
scanner tac ebean col myba ram figure fin expressed 1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating b
使用dubbo對外暴露介面,實現類同時實現兩個介面後 @Autowire失敗,提示expected single matching bean but found 2解決方案
當使用dubbo對外暴露介面時,為了方便繼承和實現底層方法,另一方面專門對外提供一套介面方法,這時介面實現類同時實現了兩個介面: @Service("ICentAccountInfoService") public class CentAccountIn
Spring Boot +Mybatis 多資料來源的配置和使用
1、在application.properties中新增資料庫連線配置 mybatis.type-aliases-package=com.yc.edusys.bean mybatis.mapper-locations=cla
mybatis多資料來源報錯
2018-12-06 16:58:35,709 [ main ] - [ INFO ] [ org.springframework.core.KotlinDetector : 57 ] - Kotlin reflection implementation not found a
spring-boot-mybatis-多資料來源
sql 語句 DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵id', `userName` varchar(32) DEFAULT NUL
Spring Boot + Mybatis 多資料來源配置實現讀寫分離
本文來自網易雲社群作者:王超應用場景:專案中有一些報表統計與查詢功能,對資料實時性要求不高,因此考慮對報表的統計與查詢去操作slave db,減少對master的壓力。根據網上多份資料測試發現總是使用master資料來源,無法切換到slave,經過多次除錯修改現已完美通過,現
Spring boot(七):Spring boot+ mybatis 多資料來源最簡解決方案
多資料來源一般解決哪些問題?主從模式或者業務比較複雜需要連線不同的分庫來支援業務。 直接上程式碼。 配置檔案 pom包依賴,該依賴的依賴。主要是資料庫這邊的配置: mybatis.config-locations=classpath:mybatis/mybati
Spring Boot+Mybatis多資料來源讀寫分離
兩天搭建了一個SpringBoot,Mybatis多資料來源讀寫分離,redis實現session共享的例子。記錄一下~~~ 前提條件:Spring Boot,Mybatis 單資料庫能正常執行 一、Spring Boot整合Mybatis實現讀寫分離(後續會做Mysql
spring boot mybatis 多資料來源異常
摘要:在做多資料來源專案改造的時候,通過spring boot啟動applicationContext.xml配置檔案,老提示如下異常資訊: [2016-11-30 19:49:29 WARN o.s.c.a.AnnotationConfigApplicationCont
spring boot mybatis多資料來源,後端進行資料庫讀寫分離
2.後端的讀寫分離很簡單,主要是配置資料庫的難題。為啥簡單?只要資料庫配置好,你想哪個資料庫寫,哪個資料庫讀,自己安排 1)看下application.properties #spring.datasource.name= test #spring.datasource.
解決 idea 多模組 spring boot jsp頁面 404報錯
一 、報錯資訊 Tue Nov 06 20:04:30 CST 2018 There was an unexpected error (type=Not Found, status=404). /WEB-INF/jsp/index.jsp 二、問題解決
【十九】Spring Boot 之多資料來源和分散式事務(JTA、Atomikos、Druid、Mybatis)
1.事務開始 2.A資料來源insert 3.B資料來源insert 4.報錯 5.事務回滾(A、B資料來源都回滾) 專案目錄 pom.xml <?xml version="1.0" encoding="UTF-8"?> <projec
Spring Boot MyBatis 動態資料來源切換、多資料來源,讀寫分離
轉載自:https://blog.csdn.net/u013360850/article/details/78861442本專案使用 Spring Boot 和 MyBatis 實現多資料來源,動態資料來源的切換;有多種不同的實現方式,在學習的過程中發現沒有文章將這些方式和常
關於spring boot項目啟動報錯問題
系列 cnblogs mage 函數 img boot 1.8 沒有 mod 這次用了spring boot,項目在啟動時報錯,由於我用的是jdk 1.7 ,該項目默認的是jdk 1.8,所以要把下面這一系列配置改好才行。廢話不多說,上圖: 一看發現,項
Spring Boot連接MySQL報錯“Internal Server Error”的解決辦法
deb 解決辦法 pri int encoding demo could CA timezone 報錯信息如下: {timestamp: "2018-06-14T03:48:23.436+0000", status: 500, error: "Internal Server
添加spring-boot-devtools熱部署報錯
pre fig ica option tool pan 解決方案 解決 true 使用的eclipse部署的spring boot,百度了下,大部分的問題都是說IDE工具使用熱部署無法成功的解決方案,看了很懵逼 <!-- 熱部署模塊 --> <depend
spring boot 配置多資料來源
1.application.yml配置 server: port: 8088 spring: http: multipart: max-file-size: 50Mb max-request-size: 50Mb enabl
spring+springmvc+mybatis環境搭建報錯問題(二)
1.啟動程式報錯: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
引入spring-boot-starter-redis包報錯 :unknown
springboot整合redis時,引入spring-boot-starter-redis包報錯,maven找不到這個資源.如下圖: 我的專案中,spring boot是 用的2.0.4版本. spring-boot-starter-redis在spring