MongoDB & Spring MVC Integration
Post on 11-May-2015
22526 Views
Preview:
DESCRIPTION
Transcript
Hands-on NoSQL MongoDB & Spring Integration
http://automateddeveloper.blogspot.com
I’m a professional Software Engineer based in the UK.
Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB
Still want more?– http://automateddeveloper.blogspot.com– http://www.thirdmindmedia.co.uk– info@thirdmindmedia.co.uk
What is NoSQL?
NoSQL means “Not Only SQL”The NoSQL umbrella covers a range of non-
relational databases, including key-value, document , wide column store and graph based databases
NoSQL
Document
MongoDB CouchDB
Graph
Neo4J InfoGrid
Wide Column
Cassandra Hadoop
Why NoSQL?
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
Many leading web 2.0 sites now use NoSQL implementations:– Facebook – Cassandra (originally developed in-house
and later open-sourced– Twitter – Hadoop & Cassandra– Google – BigTable– Digg – Cassandra– FourSquare - MongoDB
In the News
$14millionAmount raised in Series C Funding last week by CouchBase for further investment in to NoSQL solutions(Aug2011)
4,850%Percent increase in NoSQL jobs since November 2009 as reported by SimplyHired.com
8TBAmount of data generated everyday in Twitter, and needs to be supported by their NoSQL solution (figure from 2010)
MongoDB
An Open Source Document oriented database
Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly
Supports indexing of documentsAuto-sharding supported, providing full
horizontal scaling
MongoDB & Spring
Core Spring and the SpringMVC framework areenterprise standard Javalibraries for building professional web sites
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
How Spring & MongoDB Fit Together
Servlet Engine (e.g. Tomcat)
Controller Layer
Front Controller (Spring
Dispatcher)
Controller
Request/Response
Service Layer DAO Layer
Mongo Repository
iRepository
Mongo DB
Spring enables complete configuration of the Controller, Service, DAO Layer and facilitates connection to Mongo DB
Getting Started
This overview does not cover details of setting up a Spring MVC app, and will assume a working knowledge of the framework
Only the configuration to connect an existing Spring MVC app will be covered in detail
Before proceeding, install MongoDB – full details are available: http://www.mongodb.org/display/DOCS/Quickstart
If you plan to follow the walkthrough with the source code, it is provided as an Eclipse/Maven project and uses the M2Eclipse plugin
Domain Objects
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.
Using Spring Data we can model ourdomain using POJOsand annotations.
Domain Objects
@Document annotation on the root Document object
@Id on any member variable to be used as an identifier for the object (must be String/int/long)
The @Id field must either be named “id”, or the annotation used in combination with a MappingConverter
Domain Objects 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
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)
Using JPA, all three objects would be annotated “@Entity” and all could be queried as independent objects
@Document public class Resume
@Idprivate String id;
public class ResumePage
public class PageSection
Data Access – The Repository
Core CRUD functionality can be achieved out-of-the-box by extending the “MongoRepository” class
This provides save(), findAll(), findOne() and delete() methods that can be used on our @Document object (in our case “Resume.java”)
Data Access – The Repository
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);
Criteria allows querying by any field name in a MongoDB Document
Service Layer
The majority of the MongoDB implementation is abstracted to the Data Access layer through the MongoRepository
There is no real change in the Service or Controller Layers to the normal Spring MVC approach
In the Service class we can just auto-wire the Repository Interface in to the class just like we would normally with the DAOs
Configuration
<!-- Mongo Configuration --><mongo:repositories base-package="com.tmm.nosql.mongodb.repo" />
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongoDbFactory" /></bean>
<!-- local config --><mongo:db-factory id="mongoDbFactory" dbname="resume_db“ host="localhost" port="27017"/>
1. Define package location for the Repository
2. Define MongoTemplate bean
3. Define the Mongo DB Factory with the host name, port, dbname (can also provide username and password if needed)
Deploying to CloudFoundry
Sign up for your free CloudFoundry account (currently in Beta, so it may take time for your account details) at http://www.cloudfoundry.com/
Install the latest STS Eclipse build and CloudFoundry extensions (full details here: http://blog.springsource.com/2011/04/13/using-cloud-foundry-from-sts/
App Config for CloudFoundry
A change to your application config is needed to support access to CloudFoundry MongoDB service
Notice the only change is the “mongoDbFactory” bean is being instantiated with no constructor arguments – this will all be injected by Cloud Foundry
Configuring CloudFoundryIn the Servers tab, double-click the deployed application to open the Applications tab
In the Services section of the Applications tab, select the “Add Service” button, select MongoDB and name the service appropriately
Once the MongoDB service has been created, click and drag it in to the “Application Services” tab
Thoughts
NoSQL is growing in popularity but is not intended to be a replacement for RDBMS
NoSQL should be considered as another tool in the arsenal – RDBMS is not going anywhere!
Database technology choice should be driven by the requirements of the problem – Document oriented have specific applications
Appendix
Full source code for the application is available from http://automateddeveloper.blogspot.com
Feel free to drop me an email or come by to the blog and leave a comment if you have any questions or feedback
top related