spring boot啟動報錯Error starting ApplicationContext(未能配置資料來源)

語言: CN / TW / HK

主要錯誤:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

未能配置資料來源:未指定“url”屬性,也無法配置嵌入式資料來源。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-03 09:47:45.300 ERROR 23160 ---
 [    //加入Java開發交流君樣:756584822一起吹水聊天     
  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:
 //加入Java開發交流君樣:756584822一起吹水聊天
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).

原因:

spring boot預設會載入

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類,DataSourceAutoConfiguration類使用了@Configuration註解向spring注入了dataSource bean。因為工程中沒有關於dataSource相關的配置資訊,當spring建立dataSource bean因缺少相關的資訊就會報錯。

兩種解決辦法

1.在Application類上加
`
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

2.配置資料來源:

例如在application配置檔案中:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:
//ip:埠號/資料庫名稱?
useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

image

最新2020整理收集的一些高頻面試題(都整理成文件),有很多幹貨,包含mysql,netty,spring,執行緒,spring cloud、jvm、原始碼、演算法等詳細講解,也有詳細的學習規劃圖,面試題整理等,需要獲取這些內容的朋友請加Q君樣:756584822

分享到: