Springboot Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
Posted 悟能的师兄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded相关的知识,希望对你有一定的参考价值。
一. 异常现象
我在Spring Boot中关联mysql、Mybatis进行数据库开发时,按照正常步骤添加了相关数据库的依赖,也进行了必要的数据库配置,结果在项目启动时出现如下异常信息:
***************************
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).
Process finished with exit code 1
看提示信息是说当前项目没有配置DataSource相关的配置!
二. 异常原因
其实这个异常在SpringBoot中是一个比较常见的异常,一般是因为SpringBoot自动配置时,检测到我们添加了MySQL、Oracle、Mybatis等和数据库相关的依赖包,结果我们的配置文件中却没有添加数据库相关的配置,比如:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?characterEncoding=utf-8
username: root
password: root
三. 解决办法
针对以上原因造成的异常,可以采用如下办法解决:
方式一:在application.yml/applicaiton.properties中添加数据库相关配置;
方式二:在SpringBootApplication注解中进行数据库配置的排除,即@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)。
以上是关于Springboot Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded的主要内容,如果未能解决你的问题,请参考以下文章
springboot启动报错:Failed to configure a DataSource
Springboot:解决Failed to load ApplicationContext问题
SpringBoot+WebSocket问题:Failed to register @ServerEndpoint class
SpringBoot启动异常:Reason: Failed to determine a suitable driver class