SpringBoot或者SpringCloud报错 Failed to auto-configure a DataSource: ‘spring.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 class

分析原因

1.由于我使用了Spring-Mybatis组件,但是没用引入数据源等各种信息,由于这些组件都是被自动装配进SpringBoot项目中,Spring Boot会创建默认的内存数据库的数据源DataSource,当然如果你自定义了DataSource,SpringBoot就不会自己创建了。

解决思路

1.我们可以导入数据源信息,启动数据库配置。
2.我们可以对SpringBoot组件进行关闭一些配置,不让它进行对“特定”组件的自动装配,或者说让SpringBoot忽视掉一些“组件”, 让spring-boot不要根据Maven中依赖自动配置了。

解决方案

1.使用 @SpringBootApplication 注解,用 exclude 属性进行排除指定的类

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    // ...
}

2.单独使用 @EnableAutoConfiguration 注解的时候

当然如果你使用了 @SpringBootApplication最好还是在这里面进行排除。

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    // ...
}

3.使用 @SpringCloudApplication 注解的时候:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringCloudApplication
public class Application {
    // ...
}

4.终极方案,不管是 Spring Boot 还是 Spring Cloud 都可以搞定,在配置文件中指定参数 spring.autoconfigure.exclude 进行排除

spring:     
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

或者

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小小狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值