Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MongoDB & Spring MVC Integration

Introduction to NoSQL and MongoDB along with step by step walkthrough on how to integrate MongoDB with the Spring MVC framework. Full source code available for example project from

MongoDB & Spring MVC Integration

  1. 1. Hands-on NoSQL <br />MongoDB & Spring Integration<br /><br />
  2. 2. <ul><li>I’m a professional Software Engineer based in the UK.
  3. 3. Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB
  4. 4. Still want more?</li></ul><br /><br /><br />
  5. 5. What is NoSQL?<br /><ul><li>NoSQL means “Not Only SQL”
  6. 6. The NoSQL umbrella covers a range of non-relational databases, including key-value, document , wide column store and graph based databases</li></li></ul><li>Why NoSQL?<br /><ul><li>NoSQL’s recent increase in popularity being largely driven by the explosion in amount of data that is now being created/captured on the web
  7. 7. Many leading web 2.0 sites now use NoSQL implementations:</li></ul>Facebook – Cassandra (originally developed in-house and later open-sourced<br />Twitter – Hadoop & Cassandra<br />Google – BigTable<br />Digg – Cassandra<br />FourSquare - MongoDB<br />
  8. 8. In the News<br />4,850%<br />Percent increase in NoSQL jobs since November 2009 as reported by<br />$14million<br />Amount raised in Series C Funding last week by CouchBase for further investment in to NoSQL solutions(Aug2011)<br />8TB<br />Amount of data generated everyday in Twitter, and needs to be supported by their NoSQL solution (figure from 2010)<br />
  9. 9. MongoDB<br /><ul><li>An Open Source Document oriented database
  10. 10. Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly
  11. 11. Supports indexing of documents
  12. 12. Auto-sharding supported, providing full horizontal scaling</li></li></ul><li>MongoDB & Spring<br /><ul><li>Core Spring and the SpringMVC framework areenterprise standard Javalibraries for building professional web sites
  13. 13. As more and more web sites need to be able to handle huge amounts of data, there will be an increased need to be able to build professional sites incorporating NoSQL solutions</li></li></ul><li>How Spring & MongoDB Fit Together<br /><ul><li>Spring enables complete configuration of the Controller, Service, DAO Layer and facilitates connection to Mongo DB</li></ul>Servlet Engine (e.g. Tomcat)<br />Controller Layer<br />Service Layer<br />DAO Layer<br />Request/Response<br />Front Controller (Spring Dispatcher)<br />iRepository<br />Mongo DB<br />Controller<br />Mongo Repository<br />
  14. 14. Getting Started<br /><ul><li>This overview does not cover details of setting up a Spring MVC app, and will assume a working knowledge of the framework
  15. 15. Only the configuration to connect an existing Spring MVC app will be covered in detail
  16. 16. Before proceeding, install MongoDB – full details are available:
  17. 17. If you plan to follow the walkthrough with the source code, it is provided as an Eclipse/Maven project and uses the M2Eclipse plugin</li></li></ul><li>Domain Objects<br /><ul><li>Similar to using JPA(Hibernate, etc) the first step is to model the underlying Domain objects you want to use to model the applications underlying meta-data.
  18. 18. Using Spring Data we can model ourdomain using POJOsand annotations.</li></li></ul><li>Domain Objects<br /><ul><li>@Document annotation on the root Document object
  19. 19. @Id on any member variable to be used as an identifier for the object (must be String/int/long)
  20. 20. The @Id field must either be named “id”, or the annotation used in combination with a MappingConverter</li></li></ul><li>Domain Objects<br /><ul><li>Unlike traditional JPA models, Document based models will often have a root object, and will often contain nested objects, which do not need to be annotated
  21. 21. In our Resume example, only our top level “Resume” object needs to be annotated and we will only be persisting/retrieving at this level (Document based DB makes sense for this model, as a “ResumePage” only makes sense within the context of a “Resume” and not as an object in itself)
  22. 22. Using JPA, all three objects would be annotated “@Entity” and all could be queried as independent objects</li></ul>@Document public class Resume@Idprivate String id;<br />public class ResumePage<br />public class PageSection<br />
  23. 23. Data Access – The Repository<br /><ul><li>Core CRUD functionality can be achieved out-of-the-box by extending the “MongoRepository” class
  24. 24. This provides save(), findAll(), findOne() and delete() methods that can be used on our @Document object (in our case “”)</li></li></ul><li>Data Access – The Repository<br /><ul><li>Our Repository interface can easily be extended to include custom methods, and the MongoTemplate class provides an API to create custom queries:mongoTemplate.find(“COLLECTION_NAME”, new Query(Criteria.where(“name”).is(searchTerm)), Resume.class);
  25. 25. Criteria allows querying by any field name in a MongoDB Document</li></li></ul><li>Service Layer<br /><ul><li>The majority of the MongoDB implementation is abstracted to the Data Access layer through the MongoRepository
  26. 26. There is no real change in the Service or Controller Layers to the normal Spring MVC approach
  27. 27. In the Service class we can just auto-wire the Repository Interface in to the class just like we would normally with the DAOs</li></li></ul><li>Configuration<br /><!-- Mongo Configuration --><br /><mongo:repositories base-package="com.tmm.nosql.mongodb.repo" /><br /><bean id="mongoTemplate" class=""><br /> <constructor-arg ref="mongoDbFactory" /><br /></bean><br /><!-- local config --><br /><mongo:db-factory id="mongoDbFactory" dbname="resume_db“<br /> host="localhost" port="27017"/><br />1. Define package location for the Repository<br />2. Define MongoTemplate bean <br />3. Define the Mongo DB Factory with the host name, port, dbname (can also provide username and password if needed)<br />
  28. 28. Deploying to CloudFoundry<br /><ul><li>Sign up for your free CloudFoundry account (currently in Beta, so it may take time for your account details) at
  29. 29. Install the latest STS Eclipse build and CloudFoundry extensions (full details here:</li></li></ul><li>App Config for CloudFoundry<br /><ul><li>A change to your application config is needed to support access to CloudFoundry MongoDB service
  30. 30. Notice the only change is the “mongoDbFactory” bean is being instantiated with no constructor arguments – this will all be injected by Cloud Foundry</li></li></ul><li>Configuring CloudFoundry<br />In the Servers tab, double-click the deployed application to open the Applications tab<br />In the Services section of the Applications tab, select the “Add Service” button, select MongoDB and name the service appropriately<br />Once the MongoDB service has been created, click and drag it in to the “Application Services” tab<br />
  31. 31. Thoughts<br /><ul><li>NoSQL is growing in popularity but is not intended to be a replacement for RDBMS
  32. 32. NoSQL should be considered as another tool in the arsenal – RDBMS is not going anywhere!
  33. 33. Database technology choice should be driven by the requirements of the problem – Document oriented have specific applications</li></li></ul><li>Appendix<br /><ul><li>Full source code for the application is available from
  34. 34. Feel free to drop me an email or come by to the blog and leave a comment if you have any questions or feedback</li>

    Be the first to comment

    Login to see the comments

  • iworkf3

    Nov. 2, 2011
  • harishamys

    Jan. 13, 2012
  • sp3ctrum

    Feb. 6, 2012
  • wai277

    Feb. 17, 2012
  • gotomypc1

    Mar. 12, 2012
  • coolhero

    Apr. 17, 2012
  • RePeter1

    May. 29, 2012
  • akazlou

    Dec. 9, 2012
  • UrbanWalks

    Jan. 17, 2013
  • itsme_seenu

    Mar. 4, 2013
  • sanjutom

    Mar. 25, 2013
  • davidcyp

    Apr. 19, 2013
  • vinitbothra

    Apr. 30, 2013
  • highgarden

    Jun. 17, 2013
  • Syed_Shahul

    Jun. 29, 2013
  • lacoskokan

    Nov. 10, 2014
  • WongChungHang

    Dec. 1, 2014
  • AvijitDhar1

    Jul. 31, 2015
  • dkstanaka

    Oct. 13, 2015
  • shanugandhi7

    Jun. 11, 2017

Introduction to NoSQL and MongoDB along with step by step walkthrough on how to integrate MongoDB with the Spring MVC framework. Full source code available for example project from


Total views


On Slideshare


From embeds


Number of embeds