Reason: Failed to determine a suitable driver class 项目启动报错解决
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
今天遇到了这个问题,因为在网关服务的pom.xml文件中引用了其他模块,而其他模块有DataSource相关的依赖,我的配置文件中没有对应的配置,所以报错了。顺便总结一下吧
报错信息如下:
2021-04-01 10:47:19.255 ERROR 3249 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
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 0
排查错误的原因:
情况一:服务没有用到DataSource,但是pom.xml中引入了jdbc或者 Mybatis相关的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
情况二:服务没有用到DataSource,pom.xml引入其他模块,而其他模块中有jdbc或者和mybatis相关的依赖.
比如网关模块中引入了leadstore-user这个模块,而leadstore-user这个依赖里面有mybatis-plus相关的依赖,网关的application.yml文件中又没有配置DataSource
<dependency>
<groupId>com.heima</groupId>
<artifactId>leadstore-user</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
这样的话,在springboot工程启动的时候回自动初始化DataSource相关的信息,但是却找不到,则会报错
解决方案:
方案一:1. 首先检查第一种情况,排查pom.xml中的依赖,把jdbc和mybatis相关的依赖删除;如果是需要用到的,在配置文件中添加相对应的配置,如果有添加了的话,检查一下url有没有错误
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/leadstore_user?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=true
username: root
password: root
另外需要注意一下的:
1. jar包可能冲突:使用maven-helper检查jar包是否冲突,冲突就更换下jar包版本
2. MySQL版本过高的话,application.yml文件中配置 url: &useSSL=false 会有没用的情况
方案二:不在application.yml中配置,在启动类的@SpringBootApplication加上(推荐):
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
或者在application.properties里配置:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
大概这样,遇到其他问题再回来补充。
更多推荐
所有评论(0)