Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. ...
錯誤日誌:
2019-07-13 16:04:26.318 INFO 21144 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-07-13 16:04:26.325 WARN 21144 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43) 2019-07-13 16:04:26.333 INFO 21144 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-07-13 16:04:26.339 ERROR 21144 --- [ main] 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). Disconnected from the target VM, address: '127.0.0.1:7870', transport: 'socket' Process finished with exit code 1
問題分析及解決方案
問題原因: Mybatis沒有找到合適的載入類,其實是大部分spring - datasource - url沒有載入成功,分析原因如下所示.
-
DataSourceAutoConfiguration會自動載入.
-
沒有配置spring - datasource - url 屬性.
-
spring - datasource - url 配置的地址格式有問題.
-
配置 spring - datasource - url的文件沒有載入.
- profiles文件夾名稱衝突
方案一 (解決原因1)
排除此類的autoconfig。啟動以後就可以正常運行。
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
但是這個顯然是治標不治本的,除非你不打算引用mybatis。
方案二 (解決原因2)
在application.properties/或者application.yml文件中沒有添加資料庫配置信息.
spring: datasource: url: jdbc:mysql://localhost:3306/read_data?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
方案三(解決原因3)
spring.datasource.url = jdbc:mysql://192.168.0.20:1504/f_me?setUnicode=true&characterEncoding=utf8
方案四 (解決原因4)
yml或者properties文件沒有被掃描到,需要在pom文件中<build></build>添加如下.來保證文件都能正常被掃描到並且載入成功.
<!-- 如果不添加此節點mybatis的mapper.xml文件都會被漏掉。 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
方案五 (解決原因5)
以上4個我逐一嘗試了,仍然沒能解決問題,於是想到會不會是profiles文件夾名稱問題,將profiles修改成config,問題解決。
如圖:
報錯時:
解決後:
特別感謝以下鏈接地址,以上部分信息直接借鑒的以下地址中的內容:
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
- SpringBoot 2.0 報錯: Failed to configure a DataSource: 'url' attribute is not specified and no embe...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. SPRING
- Resolving “Failed to Configure a DataSource” Error
v源碼地址
https://github.com/toutouge/javademosecond/tree/master/hellolearn
作 者:請叫我頭頭哥
出 處:http://www.cnblogs.com/toutou/
關於作者:專註於基礎平臺的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回覆。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角【推薦】一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!