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. 总结问题
出现上述错误,一般有如下解决方案:
-
查看是否缺少了驱动类。
-
application.yml
的文件是否配置有误:-
datasource
的相关属性是否配置有误,例如:地址值啊,数据库驱动啊,用户名啊,密码啊。 -
指定文件(
@spring.active@
)是否有误。
-
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99117.html