【转】使用Spring Data来操作MongoDB(单实例)(2)
创建Spring context。将所有spring beans和mongodb对象都声明在Spring context文件中,这里创建的是applicationContext.xml文件。注意到我们并没有创建一个叫做"nature"的数据库。在第一次存储数据的时候MongoDB将会为我们创建这个数据库。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="natureRepository"
class="com.orangeslate.naturestore.repository.NatureRepositoryImpl">
<property name="mongoTemplate" ref="mongoTemplate" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="nature" />
</bean>
<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
<property name="port" value="27017" />
</bean>
<!-- Activate annotation configured components -->
<context:annotation-config />
<!-- Scan components for annotations within the configured package -->
<context:component-scan base-package="com.orangeslate.naturestore">
<context:exclude-filter type="annotation"
expression="org.springframework.context.annotation.Configuration" />
</context:component-scan>
</beans>
创建一个测试类。这里我已经创建了一个测试类,并通过ClassPathXmlApplicationContext来初始化他。
package com.orangeslate.naturestore.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.orangeslate.naturestore.domain.Tree;
import com.orangeslate.naturestore.repository.NatureRepositoryImpl;
import com.orangeslate.naturestore.repository.Repository;
public class MongoTest {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:/spring/applicationContext.xml");
Repository repository = context.getBean(NatureRepositoryImpl.class);
// cleanup collection before insertion
repository.dropCollection();
// create collection
repository.createCollection();
repository.saveObject(new Tree("1", "Apple Tree", 10));
System.out.println("1. " + repository.getAllObjects());
repository.saveObject(new Tree("2", "Orange Tree", 3));
System.out.println("2. " + repository.getAllObjects());
System.out.println("Tree with id 1" + repository.getObject("1"));
repository.updateObject("1", "Peach Tree");
System.out.println("3. " + repository.getAllObjects());
repository.deleteObject("2");
System.out.println("4. " + repository.getAllObjects());
}
}
最后,让我们以Java应用程序的方式运行这个示例,我们可以看到如下的输出。第一个方法存储了一个"Apple Tree"。第二个方法存储了一个"Orange Tree"。第三个方法通过id获取一个对象。第四个使用Peach Tree更新对象。最后一个方法删除了第二个对象。
1. [Person [id=1, name=Apple Tree, age=10, category=null]] 2. [Person [id=1, name=Apple Tree, age=10, category=null], Person [id=2, name=Orange Tree, age=3, category=null]] Tree with id 1Person [id=1, name=Apple Tree, age=10, category=null] 3. [Person [id=1, name=Peach Tree, age=10, category=null], Person [id=2, name=Orange Tree, age=3, category=null]] 4. [Person [id=1, name=Peach Tree, age=10, category=null]]
注:可以在GitHub上下载到源码。

