Spring 配置#EnableAsync启动报错_SpringBoot启动报错Failed to configure a DataSource: '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 classAction: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
9af8a42deb26cf9ad15b9470376f2246.png

分析原因

根据报错日志分析是在springboot项目启动的时候没有找到database 数据库连接地址,我们知道在spring boot 启动的时候会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。

184cd23ff0e2f0e43a549c565e055f85.png

问题排查

  1. 检查pom.xml 项目数据库jar 是否引用;
  2. 查看.properties或.yml 配置文件是否配置数据库链接池;
  3. 查看spring - datasource - url 配置的地址格式错误需要转义等;
  4. yml或者properties文件可能没有被扫描到(情况比较少,如果按照标准命名都会被默认扫描);

解决方案

方案1、如果项目不需要数据库相关信息就排除此类的autoconfig

在@SpringBootApplication注解上加上exclude,解除自动加载DataSourceAutoConfiguration。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

springboot启动类加上这个启动以后就可以正常运行。完整代码:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)public class Application { public static void main(String[] args) { SpringApplication.run(Application, args); }}

方案2、配置文件添加数据库链接信息

**. properties 文件添加数据库配置信息(以mysql为例):

druid.driver=com.mysql.jdbc.Driverdruid.url=jdbc:mysql://localhost:3306/data?serverTimezone=Asia/Shanghai&allowMultiQueries=truedruid.username= rootdruid.password= root

**.yml文件添加数据库配置信息(已mysql为例):

spring: datasource: url: jdbc:mysql://localhost:3306/data?serverTimezone=Asia/Shanghai&allowMultiQueries=true username: root password: root driver-class-name: com.mysql.jdbc.Driver

方案3、配置信息转义

//错误示例

druid.url= jdbc:mysql://localhost: 3306/data?serverTimezone=Asia/Shanghai&allowMultiQueries=true

//正确示例

druid.url= jdbc:mysql://localhost: 3306/data?serverTimezone=Asia/Shanghai&allowMultiQueries=true

方案4、配置pom.xml 中yml或者properties扫描

需要在pom文件中

src/main/java**/*.yml**/*.properties**/*.xmlfalsesrc/main/resources**/*.yml**/*.properties**/*.xmlfalse
f0f1f935aeb8f7c58a27f5c3804b51e8.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值