springboot(2.0以上) --数据源切换时报错
Posted suc-浮生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot(2.0以上) --数据源切换时报错相关的知识,希望对你有一定的参考价值。
在进行数据源切换时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了
以上是关于springboot(2.0以上) --数据源切换时报错的主要内容,如果未能解决你的问题,请参考以下文章