org.springframework.data 1.8.1 spring 4.0.4 mongo-java-driver3.0.0 配置
<?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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd" default-lazy-init="true"> <context:component-scan base-package="com.gooddeep.mongdb.helper" /> <!-- <context:annotation-config /> --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 提供该PropertyPlaceholderConfigurer bean支持把properties文件中的信息读取到XML配置文件的表达式中 --> <!-- 从Spring2.5开始引入命名空间配置加载properties文件到XML --> <context:property-placeholder location="classpath:config/mongodb.properties" /> <mongo:mongo id="mongo" replica-set="${mongo.host}"> <mongo:options connections-per-host="${mongo.connections}" threads-allowed-to-block-for-connection-multiplier="${mongo.connectionThreads}" auto-connect-retry="true" /> </mongo:mongo> <mongo:db-factory id="dbFactory" dbname="${mongo.dbname}" mongo-ref="mongo" username="${mongo.username}" password="${mongo.password}" /> <!-- mongo模板操作对象 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="dbFactory"></constructor-arg> <property name="writeResultChecking" value="EXCEPTION"/> </bean> <!-- MongoDB GridFS Template 支持,操作mongodb存放的文件 --> <mongo:mapping-converter id="converter" db-factory-ref="dbFactory"/> <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="dbFactory"/> <constructor-arg ref="converter"/> </bean> <!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 --> <!-- <mongo:mapping-converter base-package="com.gooddeep.mongdb.helper" /> --> <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 --> <!-- <mongo:repositories base-package="com.gooddeep.mongdb.helper" /> --> <!-- <context:annotation-config /> --> </beans>
mongodb.properties配置
mongo.host=localhost mongo.port=27017 mongo.dbname=admin mongo.username=root mongo.password=123456
org.springframework.data 1.8.1 spring 4.0.4 mongo-java-driver2.13.0 配置
<?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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd" default-lazy-init="true"> <context:component-scan base-package="com.gooddeep.mongdb.helper" /> <!-- <context:annotation-config /> --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 提供该PropertyPlaceholderConfigurer bean支持把properties文件中的信息读取到XML配置文件的表达式中 --> <!-- 从Spring2.5开始引入命名空间配置加载properties文件到XML --> <context:property-placeholder location="classpath:config/mongodb.properties" /> <!-- spring连接mongodb数据库的配置 --> <mongo:mongo-client host="${mongo.host}" port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.dbname}" id="mongo"> <mongo:client-options write-concern="SAFE"/> </mongo:mongo-client> <mongo:db-factory id="dbFactory" dbname="lhy" mongo-ref="mongo" /> <!-- mongo模板操作对象 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="dbFactory"></constructor-arg> <property name="writeResultChecking" value="EXCEPTION"/> </bean> <!-- MongoDB GridFS Template 支持,操作mongodb存放的文件 --> <mongo:mapping-converter id="converter" db-factory-ref="dbFactory"/> <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="dbFactory"/> <constructor-arg ref="converter"/> </bean> <!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 --> <!-- <mongo:mapping-converter base-package="com.gooddeep.mongdb.helper" /> --> <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 --> <!-- <mongo:repositories base-package="com.gooddeep.mongdb.helper" /> --> <!-- <context:annotation-config /> --> </beans>
mongo.host=localhost:10001 mongo.connections=100 mongo.connectionThreads=50 mongo.dbname=db mongo.username=test mongo.password=123456