美文网首页spring bootspring cloud 微服务
spring boot 数据源配置问题

spring boot 数据源配置问题

作者: 大浪滔滔 | 来源:发表于2017-11-30 11:29 被阅读92次

    1、问题现象

    通过spring官网生成Spring boot项目时如果选择了数据库,或者依赖的maven modoule有依赖数据库驱动、druid等,则在启动会报类似异常:

    org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
        at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:246) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
        at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineUsername(DataSourceProperties.java:328) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
        at com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper.afterPropertiesSet(DruidDataSourceWrapper.java:44) ~[druid-spring-boot-starter-1.1.3.jar:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    

    2、问题分析

    spring boot所依赖的maven模块中含有数据库相关的组件,这些组件在初始化时会自动注入数据源,然后在配置文件中找不到数据源配置信息,因此抛出异常。

    3、解决方案

    1. 如果不需要数据库支持,将pom文件中的数据库相关的依赖删除,即可正常启动。

    2. 如果依赖的maven公共模块依赖了数据库驱动,但是本工程没有用到数据库相关的功能,则可以在本工程的@SpringBootApplication中排除数据库相关的注入

    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
    
    1. 如果本工程用到了数据库,则提供数据源的相关配置。

    相关文章

      网友评论

        本文标题:spring boot 数据源配置问题

        本文链接:https://www.haomeiwen.com/subject/etqvbxtx.html