日期:2014-05-16  浏览次数:20456 次

spring集成MongoDB配置
spring集成MongoDB配置

一、jar包:
mongo-java-driver-2.11.1.jar
spring-data-commons-core-1.2.0.M1.jar
spring-data-mongodb-1.0.0.M4.jar

二、application-mongoDB.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: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="userCredentials" class="org.springframework.data.authentication.UserCredentials">
    	<constructor-arg name="username" value="admin"/>
    	<constructor-arg name="password" value="admin"/>
    </bean>
    
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    	<constructor-arg name="mongo" ref="mongo"/>
    	<constructor-arg name="databaseName" value="tyBGXypArPvzmXmDrPZU"/>
    	<constructor-arg name="userCredentials" ref="userCredentials"/>
    </bean>

    <bean id="mongoOptions" class="com.mongodb.MongoOptions">
    	<property name="cursorFinalizerEnabled" value="false"/>
    </bean>
    
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    	<property name="mongoOptions" ref="mongoOptions"/> 
    	<property name="host" value="localhost"/>
    	<property name="port" value="27017"/>
   </bean>
</beans>