1. 程式人生 > >Spring boot2.0 啟動報錯 Failed to auto-configure a DataSource

Spring boot2.0 啟動報錯 Failed to auto-configure a DataSource

今天Spring Boot 2.0正式版釋出,尋思著搭個小demo嘗試一下Spring Boot的新特性,使用idea建立專案。在選擇元件時添加了mysql、mybatis 然後在第一次啟動的時候啟動報錯,錯誤資訊如下:

APPLICATION FAILED TO START

Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-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會去讀取資料來源配置,而我新建的專案還沒有配置資料來源,所以會導致異常出現。

即:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})