To start developing applications with MongoDB, you need to install a driver for your chosen programming language. I select Java here.
There you don't need to download the driver if you are using maven to build the project. Just include the following dependency to the parent pom.
<Your Language> -> Releases -> Installation will give the dependency that should be added.
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.14.2</version> </dependency> </dependencies>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.12.1.RELEASE</version> </dependency>
But remember if you are including the below dependency, you don't need to put the dependency for the mongo-java-driver above. That is because it is internally using the mongo java driver to perform database operations.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.9.1.RELEASE</version> </dependency>
I will be excluding the mongo-java-driver dependency here and using the dependency for spring-data-mongodb instead along with the spring-data-commons dependency above.
One of the first things to do here is to create a com.mongodb.Mongo object using the spring's IoC container. There are several approaches to do this. But here we will be using the XML based bean metadata.
Add the following lines to the servlet.xml file in your spring project under the <beans> configuration.
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
The following 2 lines should go under the xsi:schemaLocation attribute in the <beans> configuration.
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
That is in this way for your further clarification. Look at the lines which are in bold.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
Then, include the following lines into the same file.
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="<NAME-OF-YOUR-DB>" />
</bean>
Now add the following bean configuration into the same file. This defines a post processor to translate any mongodb exception thrown in @Repository annotated classes.
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
That's it to be added to the servlet.xml file.
You don't have to do any changes to the web.xml file with regard to the integration of MongoDB to your Spring application.
So now you are done with the basic setup of MongoDB integration.
The next thing is to use MongoDB queries in your code to perform say CRUD operations on your Database. That could be achieved through DAO and DAOImpl classes and the controller to call the methods in the DAO.
The way to do this is explained in another blog post of mine here.
Comments
Post a Comment