SpringBoot | 第九章:Spring boot 資料來源未配置,啟動異常
1、問題
在使Springboot自動生成的專案框架時如果選擇了資料來源,比如選擇了mysql依賴,生成專案之後,在沒有任何的配置時啟動會報一下異常,執行程式後,控制檯輸出錯誤日誌:
2018-12-04 14:00:46.890 WARN 6592 --- [ 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:142)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2018-12-04 14:00:46.924 INFO 6592 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-04 14:00:46.928 ERROR 6592 --- [ 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).
2、SpringBoot啟動類
package com.thinkingcao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* <pre>
* @author cao_wencao
* @date 2018年12月4日 下午1:59:57
* </pre>
*/
@SpringBootApplication
public class MybatisAndDruidApplication {
/**
* <pre>
* @author cao_wencao
* @param args
* </pre>
*/
public static void main(String[] args) {
SpringApplication.run(MybatisAndDruidApplication.class, args);
}
}
3、分析原因
這是因為spring boot預設會載入org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類,DataSourceAutoConfiguration類使用了@Configuration註解向spring注入了dataSource bean。因為工程中沒有關於dataSource相關的配置資訊,當spring建立dataSource bean因缺少相關的資訊就會報錯。
因為我僅僅只是使用spring boot來寫一些很簡單的例子來學習它,在Application類上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
阻止spring boot自動注入dataSource bean
4、解決辦法,二選其一
方式一: 在Springboot啟動註解里加排除裝配DataSource資料來源, @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
package com.thinkingcao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* <pre>
* @author cao_wencao
* @date 2018年12月4日 下午1:59:57
* </pre>
*/
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class MybatisAndDruidApplication {
/**
* <pre>
* @author cao_wencao
* @param args
* </pre>
*/
public static void main(String[] args) {
SpringApplication.run(MybatisAndDruidApplication.class, args);
}
}
方式一:在@EnableAutoConfiguration裡排除裝配資料來源,@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
package com.thinkingcao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* <pre>
* @author cao_wencao
* @date 2018年12月4日 下午1:59:57
* </pre>
*/
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MybatisAndDruidApplication {
/**
* <pre>
* @author cao_wencao
* @param args
* </pre>
*/
public static void main(String[] args) {
SpringApplication.run(MybatisAndDruidApplication.class, args);
}
}