Spring引导mongodb autoconfigure抛出异常“无法确定数据库类型为NONE的嵌入式数据库驱动程序类”

时间:2017-12-06 23:55:42

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

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

我的 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 >>

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

@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的驱动程序类名设置什么值?

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

1 个答案:

答案 0 :(得分:0)

尝试在主要课程中排除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配置。