完美解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource的问题
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@
)是否有误。
-
相关文章
- Windows下解决github push failed (remote: Permission to userA/XXXX.git denied to userB.) 上传gitHub失败报错
- 【COCOS2DX-LUA 脚本开发之十三】解决COCOS2DX-LUA编译到ANDROID找不到CCLUAENGINE、HELLOWORLD或出现GET DATA FROM FILE(XXX.LUA) FAILED/CAN NOT GET FILE DATA OF XXX.LUA、COCOS2DX
- eclipse中"Tomcat v8.0 Server at localhost failed to start"解决以及安装方法
- 宇视摄像机/NVR OCX插件插件安装出现:Failed to register ocx, error code 14001 错误的解决方法
- 成功解决 raise RuntimeError(“The JPMML-SkLearn conversion application has failed. The Java executable
- 成功解决NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directo
- 成功解决import win32api, sys, osImportError: DLL load failed: 找不到指定的模块。
- 成功解决NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directo
- 成功解决NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directo
- 成功解决urllib.error.URLError urlopen error Errno 11004 getaddrinfo failed
- 成功解决OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0) in cv::resize, file C:proj
- 成功解决OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2 / cairo.so.2
- 【已解决】linux下mysql报Failed to restart mysqld.service: Unit not found
- 已解决Message: unknown error: Chrome failed to start: crashed (chrome not reachable)
- “java.lang.IllegalArgumentException: Failed to evaluate expression ‘ROLE_USER’”报错的解决
- centos7 nginx Failed to read PID from file /run/nginx.pid: Invalid argument 解决方法
- 启动WIFI时:equest firmware failed with error 0xfffffffe ifconfig: SIOCSIFFLAGS: Operation not permitted
- 解决git push报错error: failed to push some refs to 的问题
- 【TDengine】解决 failed to connect to server, reason: Mnode not found
- Module build failed: Error: Cannot find module ‘node-sass‘ Require stack报错解决