记录springboot项目启动报错 Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured 问题解决,详细报错信息如下:
***************************
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 (the profiles dev are currently active).
问题背景:在SpringCloud项目下新增了个子SpringBoot微服务,这个微服务只是单纯的数据处理,不需要连接数据库,所以刻意没有做数据库的关联。但是项目启动的时候就报这个错误了,很明显就是数据库的连接信息找不到,但我明明记得建项目的时候特意没有做mysql的相关依赖啊,于是就一步步排查…(SpringBoot版本是2.6+)
1.首先检索一下pom文件中有没有数据库相关的配置,关键字:mysql、mybatis、jdbc…一通查询下来,发现并没有
2.然后看一下yml中有没有数据库的配置,发现也没有
3.其他都没有数据库的元素,但是一启动的时候就报错,就只能从启动类那边下手了,最后定位到启动类注解@SpringBootApplication了,可能在启动的时候就默认加载数据库的配置,程序找不到,就报错了。只需要在注解上去除DataSourceAutoConfiguration就可以了,完美解决!@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
启动类如下:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyServerApplication {
public static void main(String[] args) {
SpringApplication.run(MyServerApplication.class, args);
}
}