在進行數據源切換時spring.datasource.type類型根據源碼所給的預設值修改後依然報錯 先看源碼:標色部分 , 就是springboot所給的數據源 , 正常來說只要在配置文件中修改type值就可以 ,但是我發現不行 依然報錯: 錯誤信息:這個錯好多人給出的答案是讓編輯器自動提示 而不 ...
在進行數據源切換時spring.datasource.type類型根據源碼所給的預設值修改後依然報錯
- 先看源碼:標色部分 , 就是springboot所給的數據源 , 正常來說只要在配置文件中修改type值就可以 ,但是我發現不行 依然報錯:
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true) static class Tomcat extends DataSourceConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.tomcat") public org.apache.tomcat.jdbc.pool.DataSource dataSource( DataSourceProperties properties) { org.apache.tomcat.jdbc.pool.DataSource dataSource = createDataSource( properties, org.apache.tomcat.jdbc.pool.DataSource.class); DatabaseDriver databaseDriver = DatabaseDriver .fromJdbcUrl(properties.determineUrl()); String validationQuery = databaseDriver.getValidationQuery(); if (validationQuery != null) { dataSource.setTestOnBorrow(true); dataSource.setValidationQuery(validationQuery); } return dataSource; } } /** * Hikari DataSource configuration. */ @ConditionalOnClass(HikariDataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true) static class Hikari extends DataSourceConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") public HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = createDataSource(properties, HikariDataSource.class); if (StringUtils.hasText(properties.getName())) { dataSource.setPoolName(properties.getName()); } return dataSource; } } /** * DBCP DataSource configuration. */ @ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp2.BasicDataSource", matchIfMissing = true) static class Dbcp2 extends DataSourceConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.dbcp2") public org.apache.commons.dbcp2.BasicDataSource dataSource( DataSourceProperties properties) { return createDataSource(properties, org.apache.commons.dbcp2.BasicDataSource.class); } }
- 錯誤信息:這個錯好多人給出的答案是讓編輯器自動提示 而不自己寫 就不會出現(顯然不行的),value值是springboot源碼上自帶的,因此也不會錯,但是為什麼會報錯呢?
Description: Failed to bind properties under 'spring.datasource.type' to java.lang.Class<javax.sql.DataSource>: Property: spring.datasource.type Value: org.apache.commons.dbcp2.BasicDataSource Origin: class path resource [application.yml]:7:11 Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<javax.sql.DataSource>] Action: Update your application's configuration
- 原因:
在導入druid數據源時,也就是外部數據源,我們必須導入相關依賴,因此我嘗試將springboot預設數據源的相關依賴也導入
- 引入(maven):
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.2.0</version> </dependency>
- 結果:看控制台輸出---nice(解決)
- 還有就是springboot2.0.2.RELEASE版本預設的數據源並不是org.apache.tomcat.jdbc.pool.DataSource,而是:com.zaxxer.hikari.HikariDataSource
因此除了com.zaxxer.hikari.HikariDataSource不用額外引入依賴,其他的數據源都要引入依賴
- http://mvnrepository.com:在這個網址中輸入要引入的依賴,粘貼到pom中就ok了