MongoDB and Spring-Data I

Since I’ve touched this topic in a previous post. It’s about time i show you some steps required in effectively using MongoDB.

First of all MongoDB offers seamless integration with a large set of programming languages, through a series of clients built for them. But in the case of this tutorial I will be talking about Java.

Minimum requirements:

  • java JDK-1.6 minimum
  • eclipse or spring tool suite (or whatever IDE you prefer here)
  • tomcat 6+ (older versions might not work as expected, and of course if you prefer other containers, feel free to use them)
  • Maven 3.x (should work with 2.x but still i would rather go with the latest and greatest)

The rest of the dependencies will be written bellow with a sample project from A-Z.

If you have STS installed you must setup a Spring MVC Project and make sure your pom.xml contains the following:

The configuration above should enable minimum usage of the Spring infrastructure required to implement a Spring MVC project with MongoDB integration.

The second step would be to configure the web.xml as bellow:

After writing the above in your web.xml you need to configure the servlet/servlets that will handle your requests.

As it can be seen in the web.xml above, the name of the servlet is appServlet, thus you must add a file under WEB-INF, either directly in a file following the naming convention appServlet-context.xml, either as shown above in a folder named appServlet containing a servlet-context.xml.

The content of the servlet-context.xml can be seen bellow:

The definition above will define what Spring Beans will be loaded in the context of your servlet. However i will not cover the contents of how and why we should use beans and dependency injection – maybe in a follow-up article.

All you need to know is that the configuration above sets the Spring MVC Project as annotation aware, defines the main folder for resources that will be used by our app (css files, javascript files, property files, etc.), defines a view filter (capable of mapping jsp or html files so that they are accessible from the Spring Controllers) and sets the component scan to a specific base-package (the component scan handles auto-wiring beans from the specified packages so that we don’t have to initialize them by hand).

Probably the last step in Part 1 and the most important if you are thorough and organized, is setting up the root-context.xml or as it is commonly known the applicationContext.xml. This context file has the sole purpose of storing configurations or beans initialization that should be used in all your servlets so that you avoid loading the same bean multiple time in each of them.

For the test application we are building the minimum required setup for now is:

This is it for setting up the initial project, however in the second part of the article i will talk more about preparing your application for Mongo Development.

1 thought on “MongoDB and Spring-Data I”

Leave a Reply

Your email address will not be published. Required fields are marked *