Embedded MongoDB With Spring Boot

Posted By : Vishal Kumar | 31-Jul-2018

While in the meantime making and testing Spring Boot applications with MongoDB as the data store, more often than not to use the lightweight  Embedded MongoDB as opposed to running an obvious server. As the embedded MongoDB continues running in memory, it is impacting snappy and will save you package of time in the midst of both headways and when running your tests whether it's in your change machine or a CI server.

I have concealed setting MongoDB in a Spring Boot application here. 

In this post, I'll talk about how to utilize installed MongoDB in a Spring Boot application. 

The Maven Dependency

Installed MongoDB downloads and starts up a genuine MongoDB case. You get the advantage of conversing with an occurrence stacked in memory with an indistinguishable ability from your generation condition. The Maven POM reliance to incorporate Embedded MongoDB is this:

 
<dependency>
 
    <groupId>de.flapdoodle.embed</groupId>
 
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
 
    <version>1.50.5</version>
 
</dependency>
 

You additionally need to incorporate the embedmongo-spring reliance that gives Spring Factory Bean to Embedded MongoDB, similar to this:

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

At long last, with this spring-boot-starter-data-mongodb reliance pulled in, you ought to be good to go to utilize installed MongoDB in your Spring Boot application.

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

The complete pom.xml is this:

 
<?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>guru.springframework</groupId>
 
    <artifactId>spring-boot-mongodb</artifactId>
 
    <version>0.0.1-SNAPSHOT</version>
 
    <packaging>jar</packaging>
 

 
 
    <name>spring-boot-mongodb</name>
 
    <description>Demo project for Spring Boot and Mongo DB</description>
 

 
 
    <parent>
 
        <groupId>org.springframework.boot</groupId>
 
        <artifactId>spring-boot-starter-parent</artifactId>
 
        <version>1.5.1.RELEASE</version>
 
        <relativePath/> <!-- lookup parent from repository -->
 
    </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>de.flapdoodle.embed</groupId>
 
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
 
            <version>1.50.5</version>
 
        </dependency>
 
        <dependency>
 
            <groupId>cz.jirutka.spring</groupId>
 
            <artifactId>embedmongo-spring</artifactId>
 
            <version>RELEASE</version>
 
        </dependency>
 
        <dependency>
 
            <groupId>org.springframework.boot</groupId>
 
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
 
        </dependency>
 
        <dependency>
 
            <groupId>org.springframework.boot</groupId>
 
            <artifactId>spring-boot-starter-thymeleaf</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>
 

 
 
    </dependencies>
 

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

 
 

 
 
</project>

The Configuration

You have to give a MongoTemplate bean to Spring Boot for your application to collaborate with the inserted MongoDB occasion. You ordinarily utilize a @Configuration class, this way:

package guru.springframework.config;
 

 
 
import java.io.IOException;
 
import cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean;
 
import org.springframework.context.annotation.Bean;
 
import org.springframework.context.annotation.Configuration;
 
import org.springframework.data.mongodb.core.*;
 
import com.mongodb.MongoClient;
 

 
 

 
 
@Configuration
 
public class MongoConfig {
 

 
 
    private static final String MONGO_DB_URL = "localhost";
 
    private static final String MONGO_DB_NAME = "embeded_db";
 
    @Bean
 
    public MongoTemplate mongoTemplate() throws IOException {
 
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
 
        mongo.setBindIp(MONGO_DB_URL);
 
        MongoClient mongoClient = mongo.getObject();
 
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, MONGO_DB_NAME);
 
        return mongoTemplate;
 
    }
 
}
 

In this MongoConfig class, EmbeddedMongoFactoryBean is a FactoryBean for inserted MongoDB that runs MongoDB as an oversaw procedure and uncovered a pre-designed case of MongoClient. 

This is all you have to do to begin utilizing Embedded MongoDB in a Spring Boot application. 

Note: By default, your application will associate with the test database. For an alternate database, set the spring.data.mongodb.database property in your application.properties design document. 

About Author

Author Image
Vishal Kumar

Vishal Kumar is Master in Computers Application. He has good technical skills in Java and always motivated to learn new things.

Request for Proposal

Name is required

Comment is required

Sending message..