One, MONGO connection configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans:beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:beans= "Http://www.springframework.org/schema/beans" xmlns:context= "Http://www.springframework.org/sche Ma/context "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:mongo=" http://www.springframework.org/ Schema/data/mongo "xmlns:task=" Http://www.springframework.org/schema/task "xsi:schemalocation=" http://www.sp Ringframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd Http://www.spri Ngframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www. Springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd Http://www.springframework . Org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd/HTTP Www.springframework.org/schema/task http://www.Springframework.org/schema/task/spring-task.xsd "> <context:component-scan base-package=" com.noah.li.re Sultful "/> <beans:bean id=" Property "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <beans:property name=" Locations "& Gt <beans:list> <beans:value>classpath:mongo.properties</beans:value> </beans: list> </beans:property> </beans:bean> <mongo:db-factory client-uri= "mongodb://${ Mongo.config.replica.set.address}/${mongo.config.database}?replicaset=${mongo.config.replica.set.name} "/> <beans:bean id= "secondarypreferredreadpreference" class= "Com.mongodb.TaggableReadPreference.SecondaryPre Ferredreadpreference "></beans:bean> <beans:bean id=" mongooperations "class=" Org.springframework.data.mongodb.core.MongoTemplate "> <beans:constructor-arg name=" mongodbfactory "ref=" MongoDbfactory "/> <beans:property name=" readpreference "ref=" Secondarypreferredreadpreference "/> </beans: bean> <beans:bean id= "mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" > <b Eans:constructor-arg name= "Mongodbfactory" ref= "Mongodbfactory"/> </beans:bean> <!--<task:annotatio N-driven/>--></beans:beans>
Two, library configuration
Mongo.config.replica.set.address=192.168.1.xxx:27017,192.168.1.113.xxx:27018,192.168.1.xxx : 27019mongo.config.replica.set.name=replmongo.config.database=testmongo.config.username=mongo.config.password= mongodb.connection=
Spring-data-mongo-1.8.2.release Connect MongoDB Replica set memo