zl程序教程

您现在的位置是:首页 >  工具

当前栏目

完美解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource的问题

Failed 解决 to not and is No url
2023-09-14 09:07:24 时间

1. 复现问题

今天在启动项目时,遇到如下问题:

***************************
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:64983', transport: 'socket'

Process finished with exit code 0

从而,导致项目无法启动:

在这里插入图片描述

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

2. 分析问题

我们将这句错误Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.翻译为配置数据源失败:“url”属性未指定,无法配置嵌入数据源

但是,我明明在applicaltion.yml中指定了数据源的URL,如下代码所示:

spring:
 # 指定哪个文件,比如dev.yml local.yml
  config:
    activate:
      on-profile:
        - @spring.active@
  #  应用名称
  application:
    name: lowCode
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: 123456
    username: root
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
     # 连接池配置
    druid:
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM sys_user
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

我是不是哪里配置有误?别急,我们再看这句话Reason: Failed to determine a suitable driver class,将其翻译成中文是原因:无法确定合适的驱动程序类

但我已经配置了连接mysql的驱动类,如下代码所示:

<properties>
    <java.version>1.8</java.version>
    <mybatis-version>2.2.2</mybatis-version>
</properties>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-version}</version>
</dependency>

根据网上资料,说需要配置如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

但是,即便配置了该依赖,项目还是无法启动。

于是,再次去看application.yml,忽然想起来,我配置了druid连接池,但没有引入druid依赖。

3. 解决问题

正因为没有引入druid依赖,才报出了上述错误,进而引入如下依赖:

<properties>
	<java.version>1.8</java.version>
	<alibabaDruidStarter.version>1.2.11</alibabaDruidStarter.version>
</properties>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>${alibabaDruidStarter.version}</version>
</dependency>

但是,项目还是无法启动,报出同样的错误。

经过反复调试,原来我的上述applicaltion.yml文件配置出现错误,不应该使用如下代码配置active文件:

spring:
 # 指定哪个文件,比如dev.yml local.yml
  config:
    activate:
      on-profile:
        - @spring.active@

而是配置成如下代码:

spring:
 # 指定哪个文件,比如dev.yml local.yml
  profiles:
    active: @spring.active@

这两种不同配置的区别,可以参考我的这篇文章: https://blog.csdn.net/lvoelife/article/details/126350747

如是修改后,便可成功启动了项目:

在这里插入图片描述

【备注】:对spring.active不了解的,可以网上自行查找资料。

4. 总结问题

出现上述错误,一般有如下解决方案:

  1. 查看是否缺少了驱动类。

  2. application.yml的文件是否配置有误:

    • datasource的相关属性是否配置有误,例如:地址值啊,数据库驱动啊,用户名啊,密码啊。

    • 指定文件(@spring.active@)是否有误。