天天看點

SpringBoot 引入dynamic和druid找不到資料源問題

分享知識 傳遞快樂

因為項目需要的原因在 pom.xml 中引入:

<!-- 多資料源 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>

<!-- druid 官方 starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>      

但在項目啟動時提示找不資料源,出現以下錯誤提示:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (the profiles test are currently active).      

資料源明明已經正确配置了,怎麼會出現找不到資料源?

原因

SpringBoot 啟動時自動配置資料庫資訊導緻。

因為 DruidDataSourceAutoConfigure 在 DynamicDataSourceAutoConfiguration 之前會注入一個 DataSourceWrapper,會在原生的 spring.datasource 下找 url,username,password 資訊。

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)