SpringBoot啟動報錯:Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and
今天倒騰了一下自己的SpringBoot demo的程式碼,發現啟動報瞭如下的錯誤:
***************************
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).
Disconnected from the target VM, address: '127.0.0.1:64670', transport: 'socket'
上面還有一條異常資訊:
2018-08-16 00:01:40.097 WARN 12424 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 20
綜合來看確定是在專案中使用了資料庫相關的元件,但是沒有配置資料來源資訊。看了一下專案pom檔案,不知道自己什麼時候引入了spring-boot-starter-jdbc這個依賴包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
解決問題方法,如果你需要用到資料庫元件,那麼就需要配置一下資料來源相關的資訊;如果暫時不需要資料庫元件,那麼可以在專案啟動類上,這樣寫@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})就可以了;或者就是註釋掉pom檔案中用到的資料庫相關starter元件引入,重新更新一下maven專案依賴也可以。