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

2022-10-17,,,

错误日志:

2019-07-13 16:04:26.318  info 21144 --- [           main] o.apache.catalina.core.standardservice   : stopping service [tomcat]
2019-07-13 16:04:26.325  warn 21144 --- [           main] o.a.c.loader.webappclassloaderbase       : the web application [root] appears to have started a thread named [abandoned connection cleanup thread] but has failed to stop it. this is very likely to create a memory leak. stack trace of thread:
 java.lang.object.wait(native method)
 java.lang.ref.referencequeue.remove(referencequeue.java:143)
 com.mysql.jdbc.abandonedconnectioncleanupthread.run(abandonedconnectioncleanupthread.java:43)
2019-07-13 16:04:26.333  info 21144 --- [           main] conditionevaluationreportlogginglistener : 

error starting applicationcontext. to display the conditions report re-run your application with 'debug' enabled.
2019-07-13 16:04:26.339 error 21144 --- [           main] o.s.b.d.loggingfailureanalysisreporter   : 

***************************
application failed to start
***************************

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 (no profiles are currently active).

disconnected from the target vm, address: '127.0.0.1:7870', transport: 'socket'

process finished with exit code 1

问题分析及解决方案

问题原因: mybatis没有找到合适的加载类,其实是大部分spring - datasource - url没有加载成功,分析原因如下所示.

  1. datasourceautoconfiguration会自动加载.

  2. 没有配置spring - datasource - url 属性.

  3. spring - datasource - url 配置的地址格式有问题.

  4. 配置 spring - datasource - url的文件没有加载.

  5. profiles文件夹名称冲突
方案一 (解决原因1)

排除此类的autoconfig。启动以后就可以正常运行。

@springbootapplication(exclude= {datasourceautoconfiguration.class})

但是这个显然是治标不治本的,除非你不打算引用mybatis。

方案二 (解决原因2)

在application.properties/或者application.yml文件中没有添加数据库配置信息.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/read_data?useunicode=true&characterencoding=utf-8&usessl=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.driver
方案三(解决原因3)
spring.datasource.url = jdbc:mysql://192.168.0.20:1504/f_me?setunicode=true&characterencoding=utf8
方案四 (解决原因4)

yml或者properties文件没有被扫描到,需要在pom文件中<build></build>添加如下.来保证文件都能正常被扫描到并且加载成功.

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
方案五 (解决原因5)

以上4个我逐一尝试了,仍然没能解决问题,于是想到会不会是profiles文件夹名称问题,将profiles修改成config,问题解决。

如图:

报错时:

解决后:

特别感谢以下链接地址,以上部分信息直接借鉴的以下地址中的内容:

  • failed to configure a datasource: 'url' attribute is not specified and no embedded datasource could be configured
  • springboot 2.0 报错: failed to configure a datasource: 'url' attribute is not specified and no embe...
  • failed to configure a datasource: 'url' attribute is not specified and no embedded datasource could be configured. spring
  • resolving “failed to configure a datasource” error

v源码地址

作  者:

出  处:

关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者我

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!

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

下载本文的Word格式文档,以方便收藏与打印。