Get Started With MongoDB with Spring Data (1.0.1 GA Release)

Start off by getting yourself a MongoDB, I got mine hosted on MongoLab for free. Once your account is set up, create a test database. In this example I’ve simply called it “dbname”.

1. Maven Dependencies:

First setup your maven dependencies to download the required jars, or do it yourself manually. With every version of its release spring has moved the classes around, so be sure to get 1.0.1.GA Release when following this tutorial.

<dependency>
	<groupId>org.springframework.data</groupId>
 	<artifactId>spring-data-mongodb</artifactId>
 	<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.mongodb</groupId>
	<artifactId>mongo-java-driver</artifactId>
	<version>2.7.2</version>
	</dependency>
<dependency>
	<groupId>cglib</groupId>
	<artifactId>cglib</artifactId>
	<version>2.2</version>
</dependency>

 

2. Spring Release Repository:

Add the release repository to your pom.xml under repositories, or alternatively add it to your settings.xml.

<repository>
    <id>spring-release</id>
    <name>Spring Maven Release Repository</name>
    <url>http://repo.springsource.org/libs-release</url>
</repository>

 

3. Spring context.xml:

Copy the following basic configurations for spring, replace the properties with your own values from your mongo setup. If you are using MongoLab, you will be given the connection URL after you create your database, in the form of ds0XXXXX.mongolab.com

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
			http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

	<context:annotation-config />

<!-- MySQL DB Configuration -->
<util:map id="dbProps">
    <entry key="db.mongo_username" value="dbuser"/>
    <entry key="db.mongo_host" value="ds0XXXXX.mongolab.com"/>
    <entry key="db.mongo_port" value="XXXXX"/>
    <entry key="db.mongo_dbname" value="dbname"/>
	<entry key="db.mongo_password" value="dbname"/>
</util:map>

<bean>
    <property name="properties" ref="dbProps"/>
</bean>

<!-- Mongo DB Configuration -->
<mongo:db-factory id="mongoDbFactory"
			host="${db.mongo_host}"
			port="${db.mongo_port}"
			dbname="${db.mongo_dbname}"
            username="${db.mongo_username}"
            password="${db.mongo_password}"/>

<bean id="mongoTemplate">
	<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

<bean id="mongoHandler">
	<property name="mongoOperation" ref="mongoTemplate"/>
</bean>
</beans>

 

4. Java Class:

import org.springframework.data.mongodb.core.MongoOperations;

public class MongoHandler {
private MongoOperations mongoOperation;

  public void setMongoOperation(MongoOperations mongoOperation){
    this.mongoOperation = mongoOperation;
  }

  public void createCollection(String collectionName){
    mongoOperation.createCollection(collectionName);
  }

  public int getNumObjectsInCollection(String collectionName){
    return (int)mongoOperation.getCollection(collectionName).count();
  }
}