最近在学习使用spring boot。使用maven创建好工程,只引用需要用到的spring boot相关的jar包,除此之外没有任何的配置。

写了一个最简单的例子,如下所示:

 package com.torlight;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; /**
* @since 2017.05.06
* @author acer
*
*/
@SpringBootApplication
public class Application { public static void main(String[] args) {
ApplicationContext appctx= SpringApplication.run(Application.class,args); System.out.println("appctx.getBeanDefinitionCount="+appctx.getBeanDefinitionCount());
try {
((ConfigurableApplicationContext)appctx).close();
} catch (Exception e) { /*ignore*/ }
}
}

运行程序后,控制台输出错误日志:

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

 package com.torlight;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; /**
* @since 2017.05.06
* @author acer
*
*/
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class Application { public static void main(String[] args) {
ApplicationContext appctx= SpringApplication.run(Application.class,args); System.out.println("appctx.getBeanDefinitionCount="+appctx.getBeanDefinitionCount());
try {
((ConfigurableApplicationContext)appctx).close();
} catch (Exception e) { /*ignore*/ }
}
}

最新文章

  1. C语言-结构体struct-联合体union-枚举enum
  2. [Django]模型学习记录篇--基础
  3. WNDR3700V4 安装SVN Server
  4. BUG级别(优先级、严重级)定义
  5. JDK1.5新特性(一)……Enhanced for Loop
  6. Django Admin 简单部署上线
  7. wamp安装
  8. java多线程的实现的两种方法
  9. CVT电子集团--笔试部分试题
  10. ipcs、ipcrm、sysresv、kernel.shmmax
  11. C# 字符串转byte数组
  12. [JS]js中判断变量类型函数typeof的用法汇总[转]
  13. day16 十六、包、循环导入、导入模块
  14. Post和get请求时中文乱码
  15. ASP.NET Core 2.0 Preview 1 中贴心的新特性
  16. Linux 网卡流量查看
  17. JavaScript中的不可变性(Immutability)
  18. Django实现支付宝支付(沙箱)
  19. Kubuntu上连接PPTP
  20. Python读取文件编码及内容

热门文章

  1. WinInet 小例子
  2. 使用什么工具连接MySQL Server
  3. python不同package下相互调用
  4. CentOS系统中的passwd命令实用技巧小结
  5. 【MYSQL】导入中文后乱码问题
  6. meta标签整理
  7. linux系统中,tee命令的使用
  8. java编写的2048程序
  9. 超全面的JavaWeb笔记day17<JDBC>
  10. POJ 1243 One Person