Objective:
This blog is to give an insight to create the NoSQL DB configuration using spring and maven.
Pre-requisite:
To create MongoDB/NoSQL configuration, following are required.
- Running MongoDB/NoSQL DB, could be protected by user name and password
- Maven dependencies
Maven Dependency
To create configuration following dependencies are required.
1 2 3 4 5 |
<properties> <java-driver.version>3.0.3</java-driver.version> <spring.version>4.1.5.RELEASE</spring.version> <spring.data.version>1.8.0.RELEASE</spring.data.version> </properties> |
Here, I chose MongoDB as NoSQL DB therefore I used mongo driver. If you want to use other NoSQL DB then add DB specific driver dependency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>${java-driver.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${spring.data.version}</version> </dependency> |
Application.properties file
Add database information into this file as given below:
1 2 3 |
spring.data.mongodb.database=database01 spring.data.mongodb.authentication-database=admin spring.data.mongodb.uri=mongodb://test:test@localhost:27017/database01 |
NoSQL/MongoDB Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.uniquenotion.repository.RepositoryPackage; @Configuration @EnableMongoRepositories(basePackageClasses = RepositoryPackage.class) public class MongoConfig extends AbstractMongoConfiguration { @Value("${spring.data.mongodb.database}") private String dbName; @Value("${spring.data.mongodb.uri}") private String mongoURI; @Override protected String getDatabaseName() { return dbName; } @Bean public MongoDbFactory mongoDbFactory() throws Exception { return new SimpleMongoDbFactory(mongo(), getDatabaseName()); } @Bean public MongoTemplate mongoTemplate() throws Exception { MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), mappingMongoConverter()); return mongoTemplate; } @Override @Bean public Mongo mongo() throws Exception { MongoClientURI uri = new MongoClientURI(mongoURI); return new MongoClient(uri); } } |
To load all the repository classes I have added a marker interface in repository folder where all the repository present.
1 2 3 4 5 |
package com.uniquenotion.repository; public interface RepositoryPackage { } |
Now, this configuration has integrated with spring using spaing-data, mongodb driver.
If you face any difficulty to configure then leave a comment to help you out.
Git Source:
Visit git source code