Failed to configure a DataSource: 'url' attribute is not specified and no embedded 的三种解决办法

版权声明:本文为博主原创文章,如果觉得写的不错需要转载,在转载时请注明博文出处! https://blog.csdn.net/Hello_World_QWP/article/details/83271592

问题描述

在初次完成项目的构建后,启动项目时出错,提示信息“ Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. ” ,这个异常很好理解,直接翻译成中文,一看便知了 “ 配置数据源失败:没有指定'url'属性,也不能配置嵌入式数据源。 ” ,具体的错误内容如下图:

***************************
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).

如下图:

解决办法

导致这个问题的原因是因为,在 pom.xml 配置文件中,配置了数据连接技术 spring-boot-starter-jdbc 包 ,在启动配置文件时 ,Spring Boot 的自动装配机制就会去配置文件中找,相关的数据库的连接配置信息,如果找不到则抛出异常信息(具体源码就不在这儿分析了,有兴趣的可以自行去查看),

加入的 JDBC 依赖包 , 如下图:

解决办法分为三种:

第一种解决办法:

如果先暂时使用不到数据库连接,只是测试搭建的项目是否能正常启动,可以先将该配置项注释掉即可,选中该项配置按快捷键 “ Ctrl + Shift + / ” 即可。

第二种解决办法:

在 SpringBoot 应用程序启动时,排除 jdbc 的自动装配机制即可,在程序入口文件中新增配置注解 “ exclude=DataSourceAutoConfiguration.class ” ,如下图:

第三种解决办法:

在开发库或者本地库中,如果有可用的临时数据库,可以先配置该库为暂时的数据库即可,在 application.properties 配置文件中,加入如下图配置内容:

## mysql 数据连接信息
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.159.145:3306/etc_system?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = Huazai12345!

如下图:

修改配置后,测试是否能正常启动,如下图:


好了,关于 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource 的解决办法  就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者: 华    仔
联系作者: [email protected]
来       源: CSDN (Chinese Software Developer Network)
原       文: https://blog.csdn.net/Hello_World_QWP/article/details/83271592
版权声明: 本文为博主原创文章,请在转载时务必注明博文出处!

猜你喜欢

转载自blog.csdn.net/Hello_World_QWP/article/details/83271592