小编典典

配置嵌入式MongoDB的最简单方法

spring-boot

我想知道,有没有如何正确配置嵌入式任何例如MongoDBSpring Boot

例如,这就是我配置H2嵌入式数据库的方式:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.sql.DataSource;


@Configuration
@PropertySource({"configs/datasource.properties"})
public class DataSourceConfig {

    @Bean
    @Profile("test")
    public DataSource testDataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
    }
}

这很好用,但是有一个问题。这种方法不提供MongoDB的配置。

有什么解决方法吗?

更新1:

[错误]无法在项目XXX上执行目标org.springframework.boot:spring-boot-maven-
plugin:1.5.6.RELEASE:run(default-
cli):运行时发生异常。null:InvocationTargetException:创建在类路径资源[com / reborn / XXX /
config / DataSourceConfig
.class]中定义的名称为’mongoTemplate’的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframewor
k.data.mongodb.core.MongoTemplate]:工厂方法“
mongoTemplate”抛出了异常;嵌套的异常是java.lang.NoClassDefFoundError:de / flapdoodle /
embed / mongo / distribution /
IFeatureAwareVersion:de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion->
[帮助1]

更新2:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.reborn</groupId>
    <artifactId>xxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xxx</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repositories -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cz.jirutka.spring</groupId>
            <artifactId>embedmongo-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DatasourceConfig:

package com.reborn.xxx.config;

import com.mongodb.MongoClient;
import cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.io.IOException;

@Configuration
public class DataSourceConfig {

    @Bean
    public MongoTemplate mongoTemplate() throws IOException {
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
        mongo.setBindIp("localhost");
        MongoClient mongoClient = mongo.getObject();
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "abcd");
        return mongoTemplate;
    }

}

更新3:

[错误]无法在项目starbucks-finder上执行目标org.springframework.boot:spring-boot-maven-
plugin:1.5.6.RELEASE:run(default-
cli):运行时发生异常。null:InvocationTargetException:创建在类路径资源[com / reborn / xxx /
config / DataSourceConfig
.class]中定义的名称为’mongoTemplate’的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframewor
k.data.mongodb.core.MongoTemplate]:工厂方法“
mongoTemplate”抛出了异常;嵌套的异常是java.lang.IllegalArgumentException:此版本不支持32位:PRODUCTION:Windows:B32->
[帮助1]


阅读 541

收藏
2020-05-30

共1个答案

小编典典

嵌入式MongoDB的梦话可以使用Spring引导相结合。

  • 声明对flappoodle的依赖:
        <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>2.0.0</version>
    </dependency>
  • 声明对库的依赖,为flappoodle的嵌入式MongoDB提供Spring工厂bean:
        <dependency>
        <groupId>cz.jirutka.spring</groupId>
        <artifactId>embedmongo-spring</artifactId>
        <version>1.3.1</version>
    </dependency>
  • 大概,您已经声明了对的依赖spring-boot-starter-data-mongodb
        <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
  • 现在,只需MongoTemplate在嵌入式MongoDB实例上配置一个指向:
        @Bean
    public MongoTemplate mongoTemplate() throws IOException {
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
        mongo.setBindIp("localhost");
        MongoClient mongoClient = mongo.getObject();
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "test_or_whatever_you_want_to_call_this_db");
        return mongoTemplate;
    }
2020-05-30