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);
}
}