在类路径资源 + Springboot 中创建名称为“dataSource”的 bean 时出错

Posted

技术标签:

【中文标题】在类路径资源 + Springboot 中创建名称为“dataSource”的 bean 时出错【英文标题】:Error creating bean with name 'dataSource' defined in class path resource + Springboot 【发布时间】:2017-08-25 11:37:58 【问题描述】:

我正在创建一个 Springboot 应用程序,但将通过 REST 使用外部 MongoDB。我的应用程序属性文件非常简单:

application.properties

# Server
server.port=8081

我的全局应用程序文件也很简单。我使用 Parse 初始化方法连接到我的外部托管数据库:

@SpringBootApplication
@ComponentScan(basePackages = "com.test", "it.ozimov.springboot")
public class TmtApplication 

    public static void main(String[] args) 
        SpringApplication.run(TmtApplication.class, args);

        // Database connection
        Parse.initialize("applicationId","restAPIKey", "https://parseapi.back4app.com");

    

为什么会出现以下异常?

例外情况:

Caused by: 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).
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 34 more
Caused by: 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).
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 47 more
Caused by: 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).
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:246)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:183)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:42)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat.dataSource(DataSourceConfiguration.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 48 more

我对 Springboot 有什么不明白的地方?它是否正在寻找某种数据库初始化,因为它运行在 Hibernate/Tomcat 上?

【问题讨论】:

【参考方案1】:

如果您不使用数据库,请排除数据源。 @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)

you see this link

【讨论】:

【参考方案2】:

由于某种原因,@SpringBootApplication 然后@EnableAutoConfiguration 对我不起作用。

对我有用的是:

@SpringBootApplication (exclude = DataSourceAutoConfiguration.class)
@DependsOn("appConfig")
public class SerializationManager 

使用@SpringBootApplication 与分别使用@Configuration @EnableAutoConfiguration@ComponentScan 相同。

【讨论】:

以上是关于在类路径资源 + Springboot 中创建名称为“dataSource”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章

弹簧靴 |在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误

在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误

在类路径资源中定义名称为“xxxEntityManagerFactory”的 bean 创建错误

如何解决在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误?

如何在idea中创建一个SpringBoot项目(超详细教学)

如何在Dynamic CRM 2013中创建WebService接口供其它系统调用