【问题标题】:Spring boot mongodb autoconfigure throws exception "Cannot determine embedded database driver class for database type NONE"Spring boot mongodb autoconfigure 抛出异常“无法确定数据库类型 NONE 的嵌入式数据库驱动程序类”
【发布时间】:2017-12-06 23:55:42
【问题描述】:

我正在使用 Spring Boot 开发 Spring 批处理应用程序。我需要我的应用程序最终将数据写入 MongoDB,因此需要为 org.springframework.batch.item.data.MongoItemWriter 配置 org.springframework.data.mongodb.core.MongoTemplate

我的 pom.xml 依赖部分看起来像这样-

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jongo/jongo -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jvnet.jaxb2_commons/jaxb2-basics-runtime -->
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>1.11.1</version>
        </dependency>

        <!--<dependency>-->
            <!--<groupId>de.flapdoodle.embed</groupId>-->
            <!--<artifactId>de.flapdoodle.embed.mongo</artifactId>-->
            <!--<version>1.50.5</version>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->

        <!--<dependency>-->
            <!--<groupId>cz.jirutka.spring</groupId>-->
            <!--<artifactId>embedmongo-spring</artifactId>-->
            <!--<version>RELEASE</version>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.6.0</version>
        </dependency>


    </dependencies>

application.properties 文件如下所示

spring.data.mongodb.host=mongohost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=authdb
spring.data.mongodb.username=user
spring.data.mongodb.password=pwd

spring.datasource.driver-class-name=<< I don't know what to put here >>

Main 类也很简单,看起来像这样-

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

现在,每当我尝试运行我的 Main 类时,它都会出现错误

***************************
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).

在对这个问题进行了大量研究之后,我发现我需要通过在 application.properties 中提供 spring.datasource.driver-class-name 的值来让 Spring 了解我的数据存储

spring.datasource.driver-class-name=com.mongodb.Server

如果我提供 com.mongodb.Server 作为我的驱动器类名,则它在类路径中找不到并且无法识别,尽管我的类路径上有 mongo java 驱动程序依赖项。

如果我想使用mongo-java-driver,我应该为 mongoDB 的 driver-class-name 设置什么值?

如果驱动程序类名不是导致此问题的原因,那么该问题标题中提到的问题"Cannot determine embedded database driver class for database type NONE"的解决方案应该是什么?

【问题讨论】:

    标签: spring mongodb spring-boot spring-data-mongodb


    【解决方案1】:

    尝试在你的主类中排除DataSourceAutoConfiguration.class

    @SpringBootApplication
    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
    public class Main {
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
        }
    }
    

    你也不需要这个:

    spring.datasource.driver-class-name
    

    除非你也需要 jpa 配置。

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 2014-07-27
      • 2017-07-30
      • 2018-06-18
      • 2016-02-16
      • 2018-07-10
      • 2019-04-09
      • 2018-02-12
      相关资源
      最近更新 更多