报错信息如下:
***************************
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