今天遇到了這個問題,因為在網(wǎng)關(guān)服務(wù)的pom.xml文件中引用了其他模塊,而其他模塊有DataSource相關(guān)的依賴,我的配置文件中沒有對應(yīng)的配置,所以報錯了。順便總結(jié)一下吧
報錯信息如下:
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
排查錯誤的原因:
情況一:服務(wù)沒有用到DataSource,但是pom.xml中引入了jdbc或者 Mybatis相關(guān)的依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
情況二:服務(wù)沒有用到DataSource,pom.xml引入其他模塊,而其他模塊中有jdbc或者和mybatis相關(guān)的依賴.
比如網(wǎng)關(guān)模塊中引入了leadstore-user這個模塊,而leadstore-user這個依賴里面有mybatis-plus相關(guān)的依賴,網(wǎng)關(guān)的application.yml文件中又沒有配置DataSource
<dependency>
<groupId>com.heima</groupId>
<artifactId>leadstore-user</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
這樣的話,在springboot工程啟動的時候回自動初始化DataSource相關(guān)的信息,但是卻找不到,則會報錯
解決方案:
方案一:1. 首先檢查第一種情況,排查pom.xml中的依賴,把jdbc和mybatis相關(guān)的依賴刪除;如果是需要用到的,在配置文件中添加相對應(yīng)的配置,如果有添加了的話,檢查一下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里配置:文章來源:http://www.zghlxwxcb.cn/news/detail-446477.html
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
大概這樣,遇到其他問題再回來補充。文章來源地址http://www.zghlxwxcb.cn/news/detail-446477.html
到了這里,關(guān)于Reason: Failed to determine a suitable driver class 項目啟動報錯解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!