1. 程式人生 > >Spring boot專案剛搭建好,啟動測試,出現Failed to configure a DataSource

Spring boot專案剛搭建好,啟動測試,出現Failed to configure a DataSource

    今天新搭建了一個springboot+gradle框架,然而,剛搭建好執行的時候,就報了一個錯誤:
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2018-11-19 13:59:44.020 ERROR 1256 — [ 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).

    問過度娘娘後,才知道,需要在啟動類的@EnableAutoConfiguration或@SpringBootApplication中新增exclude= {DataSourceAutoConfiguration.class},排除此類的autoconfig。啟動以後就可以正常執行。
這是因為添加了資料庫元件,所以autoconfig會去讀取資料來源配置,而我新建的專案還沒有配置資料來源,所以會導致異常出現。
    照著改過之後,再次執行,就可以了,親測。。。