spring boot 启动报错: Cannot determine embedded database driver class for database type NONE

本文详细介绍了在使用SpringBoot进行简单示例开发时,遇到的由自动配置DataSource引发的异常问题及其解决方案。通过排除特定自动配置,避免了不必要的数据库相关组件加载,确保了轻量级应用的正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

最近在学习使用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*/ }
    }
}

之后即可正常启动程序 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值