最近在学习使用spring boot。使用maven创建好工程,只引用需要用到的spring boot相关的jar包,除此之外没有任何的配置。
写了一个最简单的例子,如下所示:
1 package com.torlight; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.ConfigurableApplicationContext; 9 10 11 /** 12 * @since 2017.05.06 13 * @author acer 14 * 15 */ 16 @SpringBootApplication 17 public class Application { 18 19 public static void main(String[] args) { 20 ApplicationContext appctx= SpringApplication.run(Application.class,args); 21 22 System.out.println("appctx.getBeanDefinitionCount="+appctx.getBeanDefinitionCount()); 23 try { 24 ((ConfigurableApplicationContext)appctx).close(); 25 } catch (Exception e) { /*ignore*/ } 26 } 27 }
运行程序后,控制台输出错误日志:
017-05-06 22:44:18.868 WARN 41648 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : 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$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
2017-05-06 22:44:18.871 INFO 41648 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-05-06 22:44:18.902 INFO 41648 --- [ restartedMain] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-06 22:44:18.907 ERROR 41648 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
这是因为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
1 package com.torlight; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.ConfigurableApplicationContext; 9 10 11 /** 12 * @since 2017.05.06 13 * @author acer 14 * 15 */ 16 @SpringBootApplication 17 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 18 public class Application { 19 20 public static void main(String[] args) { 21 ApplicationContext appctx= SpringApplication.run(Application.class,args); 22 23 System.out.println("appctx.getBeanDefinitionCount="+appctx.getBeanDefinitionCount()); 24 try { 25 ((ConfigurableApplicationContext)appctx).close(); 26 } catch (Exception e) { /*ignore*/ } 27 } 28 }
分类: java,spring
标签: spring boot
好文要顶 关注我 收藏该文
来自非洲大草原的食人虎
关注 - 5
粉丝 - 18
+加关注
2
2
« 上一篇:java string.format 字符串格式
» 下一篇:spring boot 扩展之AutoConfigurationImportListener
posted @ 2017-05-06 23:05 来自非洲大草原的食人虎 阅读(81879) 评论(4) 编辑 收藏
评论列表
#1楼 2017-06-01 16:32 我头像会动
用了这个注解,就包另外的错了,不好使
支持(2)反对(1)
#2楼 2017-12-15 10:38 水寒03
除了在入口加上这个注解之外,还要在自己写的controller里面加上这个注解才能正常启动
@RestController
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class IndexController {
支持(0)反对(0)
#3楼 2018-06-04 12:15 规格严格-功夫到家
en ,学习了!!
支持(0)反对(0)
#4楼 2019-02-15 14:32 weiqi101
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
报错的试下这样写