spriing boot 啓動報錯:Cannot determine embedded database driver class for database type NONE

最近在學習使用spring boot。使用maven建立好工程,只引用須要用到的spring boot相關的jar包,除此以外沒有任何的配置。spring

寫了一個最簡單的例子,以下所示:apache

 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 }

 

運行程序後,控制檯輸出錯誤日誌:tomcat

 

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 : app

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 : maven

***************************
APPLICATION FAILED TO START
***************************學習

Description:spa

Cannot determine embedded database driver class for database type NONEdebug

Action:rest

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 }
相關文章
相關標籤/搜索