zoukankan      html  css  js  c++  java
  • 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不用额外引入依赖,其他的数据源都要引入依赖

  • 相关阅读:
    删除文件夹右键下的部分目录
    c# datagridview导出到excel【转载】
    使用AO新增记录的3种方法【转载】
    AE 打包
    ArcMap 9使用技巧
    ArcEngine 渲染的使用【转载】
    关于数据库版本压缩
    SDE数据源直连
    ArcCatalog 9中的使用技巧
    AE指定字段转成注记
  • 原文地址:https://www.cnblogs.com/wangsr-suc/p/9128517.html
Copyright © 2011-2022 走看看