ZK With MongoDB Part 3 - Using Spring Data"

From Documentation
m (Created page with "{{Template:Smalltalk_Author| |author= Ashish Dasnurkar, Engineer, Potix Corporation |date=January 18, 2012 |version=ZK 5 }} {{Template:UnderConstruction}} =Introduction= For ...")
 
m
Line 26: Line 26:
 
</source>
 
</source>
  
Then add Spring data mongodb dependency in ''pom.xml'' as shown below;
+
Then add Spring data mongoDB dependency in ''pom.xml'' as shown below;
 +
 
 +
<source lang="xml">
 +
 
 +
<dependency>
 +
<groupId>org.mongodb</groupId>
 +
<artifactId>mongo-java-driver</artifactId>
 +
<version>2.6.5</version>
 +
<type>jar</type>
 +
<scope>compile</scope>
 +
</dependency>
 +
<dependency>
 +
<groupId>org.springframework.data</groupId>
 +
<artifactId>spring-data-mongodb</artifactId>
 +
<version>1.0.0.M5</version>
 +
</dependency>
 +
</source>
 +
 
 +
<li>Note that it requires mongoDB Java driver.
 +
 
 +
===View===
 +
 
 +
View part remains the same as described in earlier articles. Only change here is that the controller applied to the window is now managed by Spring so I use ZK’s spring variable resolver to resolve controller instance
 +
 
 +
<source lang="xml">
 +
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 +
<zk>
 +
<window title="Simple Todo application with Spring data support for mongodb " width="640px" border="normal" apply="${todoCtrl}">
 +
 +
</window>
 +
</zk>
 +
</source>
 +
 
 +
===Model===
 +
 
 +
There are two ways in which Spring data for mongodb can support mapping of model/domain objects. One is without using annotations and other is with annotations. Here for the sake of keeping our example code simple I will NOT use annotations so our Task model class simply consists of few fields with corresponding getter/setter methods.
 +
 
 +
<source lang="java">
 +
 
 +
public class Task {
 +
private String id;
 +
private String name;
 +
private Integer priority;
 +
private Date executionDate;
 +
 
 +
… // constructor and getter/setter methods
 +
 
 +
</source>
 +
 
 +
Now I need some supporting service that will handle CRUD of Task instances. First I need to create <tt>com.mongodb.Mongo</tt> instance and register it with Spring IoC container. Here I will use XML based bean metadata using mongo schema in a separate mongo-config.xml file to achieve this.
 +
 
 +
<source lang="xml">
 +
 
 +
<?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:p="http://www.springframework.org/schema/p"
 +
    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/data/mongo
 +
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
 +
 +
<!-- Default bean name is 'mongo' -->
 +
<mongo:mongo host="localhost" port="27017"/>
 +
 +
 
 +
</source>
 +
 
 +
As you can see from above, default connection settings for <tt>com.mongodb.Mongo</tt> instance is used. Next, <ttt>MongoTemplate</tt> is the central class in Spring data for mongoDB that provides rich set of features to interact with mongoDB database. I will use XML bean ''meatadata'' to register <tt>MongoTemplate</tt> so I can auto-wire it to use within task service class to perform CRUD operations on task mongoDB documents. Add following XML fragment in <tt>mongo-config.xml.</tt>
 +
 
 +
<source lang="xml">
 +
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
 +
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
 +
  <constructor-arg ref="mongo"/>
 +
  <constructor-arg name="databaseName" value="simpletasks"/>
 +
  </bean>
 +
</source>
 +
 
 +
I pass <tt>com.mongodb.Mongo</tt> bean to <tt>MongoTamplate</tt> and the database name. Note that <tt>MongoTemplate</tt> is thread-safe and can be reused in multiple instances.
 +
 
 +
Now let’s define our <tt>TaskService</tt> class to work with Model Task instances mapping to/from Task documents in mongoDB.
 +
 
 +
<source lang="java">
 +
 
 +
@Service("taskService")
 +
@Transactional
 +
public class TaskService {
 +
 
 +
@Resource(name="mongoTemplate")
 +
private MongoTemplate mongoTemplate;
 +
 +
 
 +
</source>
 +
 
 +
I inject <tt>MongoTempalte</tt> instance in <tt>TaskService</tt> class using <tt>@Resource</tt> annotation. Now I can use <tt>MongoTemplate</tt> instace and use its convenient APIs to access mongodb database. Let’s use it to get list of all previously created tasks in the database.

Revision as of 08:57, 12 January 2012

DocumentationSmall Talks2012JanuaryZK With MongoDB Part 3 - Using Spring Data
ZK With MongoDB Part 3 - Using Spring Data

Author
Ashish Dasnurkar, Engineer, Potix Corporation
Date
January 18, 2012
Version
ZK 5

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

Introduction

For Spring application developers, Spring Data project has support for MongoDB that allows mapping Java objects to/from mongoDB documents. In this article I will rewrite the same TODO application sample code described in Part 1 and Part 2 of this series using Spring data for mongoDB.

Using Spring Data for MongoDB

Spring Data

To include Spring Data for mongodb, add Spring milestone repository in pom.xml as we will use its latest 1.0.0.M5 milestone release

 

<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>http://maven.springframework.org/milestone</url>
		</repository>

Then add Spring data mongoDB dependency in pom.xml as shown below;

<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.6.5</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.0.0.M5</version>
		</dependency>
  • Note that it requires mongoDB Java driver.

    View

    View part remains the same as described in earlier articles. Only change here is that the controller applied to the window is now managed by Spring so I use ZK’s spring variable resolver to resolve controller instance

     
    <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
    <zk>
    <window title="Simple Todo application with Spring data support for mongodb " width="640px" border="normal" apply="${todoCtrl}"></window>
    </zk>
    

    Model

    There are two ways in which Spring data for mongodb can support mapping of model/domain objects. One is without using annotations and other is with annotations. Here for the sake of keeping our example code simple I will NOT use annotations so our Task model class simply consists of few fields with corresponding getter/setter methods.

     
    
    public class Task {
    	private String id;
    	private String name;
    	private Integer priority;
    	private Date executionDate;
    
    	// constructor and getter/setter methods
    

    Now I need some supporting service that will handle CRUD of Task instances. First I need to create com.mongodb.Mongo instance and register it with Spring IoC container. Here I will use XML based bean metadata using mongo schema in a separate mongo-config.xml file to achieve this.

     
    
    <?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:p="http://www.springframework.org/schema/p" 
        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/data/mongo
        		http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
    	
    	<!-- Default bean name is 'mongo' -->
    	<mongo:mongo host="localhost" port="27017"/>

    As you can see from above, default connection settings for com.mongodb.Mongo instance is used. Next, <ttt>MongoTemplate is the central class in Spring data for mongoDB that provides rich set of features to interact with mongoDB database. I will use XML bean meatadata to register MongoTemplate so I can auto-wire it to use within task service class to perform CRUD operations on task mongoDB documents. Add following XML fragment in mongo-config.xml.

     
    <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
      	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      			<constructor-arg ref="mongo"/>
      			<constructor-arg name="databaseName" value="simpletasks"/>
      	</bean>
    

    I pass com.mongodb.Mongo bean to MongoTamplate and the database name. Note that MongoTemplate is thread-safe and can be reused in multiple instances.

    Now let’s define our TaskService class to work with Model Task instances mapping to/from Task documents in mongoDB.

     
    
    @Service("taskService")
    @Transactional
    public class TaskService {
    
    	@Resource(name="mongoTemplate")
    	private MongoTemplate mongoTemplate;
    
    

    I inject MongoTempalte instance in TaskService class using @Resource annotation. Now I can use MongoTemplate instace and use its convenient APIs to access mongodb database. Let’s use it to get list of all previously created tasks in the database.