spring4.x和mongodb3.4集成

1 pom 依赖

     < properties >
         < project.build.sourceEncoding >UTF-8 project.build.sourceEncoding >
        
         < spring.version >4.3.2.RELEASE spring.version >
     properties >

     < dependencies >       

        
         < dependency >
             < groupId >org.springframework groupId >
             < artifactId >spring-aspects artifactId >
             < version >${spring.version} version >
         dependency >       
        
        
         < dependency >
         < groupId >org.mongodb groupId >
         < artifactId >mongo-java-driver artifactId >
         < version >3.4.2 version >
         dependency >
        
         < dependency >
         < groupId >org.springframework.da ta groupId >
         < artifactId >spring-da ta-mongodb artifactId >
         < version >1.10.4.RELEASE version >
         dependency >

     < dependency >
     < groupId >commons-beanutils groupId >
     < artifactId >commons-beanutils artifactId >
     < version >1.9.3 version >
     dependency >
     dependencies >
    

2 applicationcontext.xml
xml version= "1.0" encoding= "UTF-8" ?>
< beans xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
     xmlns= "http://www.springframework.org/schema/beans"
     xmlns:p= "http://www.springframework.org/schema/p"
     xmlns:context= "http://www.springframework.org/schema/context"
     xmlns:aop= "http://www.springframework.org/schema/aop"
     xmlns:tx= "http://www.springframework.org/schema/tx"
     xmlns:cache= "http://www.springframework.org/schema/cache"
     xmlns:task= "http://www.springframework.org/schema/task"
     xmlns:jms= "http://www.springframework.org/schema/jms"
     xmlns:core= "http://activemq.apache.org/schema/core"
     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.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.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/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.8.xsd " >    
    
    
< context:component-scan base-package= "com.code.mongdb.model" />
< context:component-scan base-package= "com.code.mongdb.dao" />
  < context:component-scan base-package= "com.code.mongdb.service" />
    
    
     < bean id= "propertyConfigurer" class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
     < property name= "locations" >
      < list >
    < value >mongo.properties value >
     list >
     property >
     bean >
id= "mongoClient" host= "${mongo.host}" port= "${mongo.port}" credentials= "${mongo.username}:${mongo.password}@${mongo.dbname}">
write-concern= "SAFE"
connections-per-host= "${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier= "${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout= "${mongo.connectTimeout}"
max-wait-time= "${mongo.maxWaitTime}"
socket-timeout= "${mongo.socketTimeout}"
/>
< mongo:db-factory id= "mongoDbFactory" dbname= "${mongo.dbname}" mongo-ref= "mongoClient" />
< bean id= "mongoTemplate" class= "org.springframework.data.mongodb.core.MongoTemplate" >
< constructor-arg name= "mongoDbFactory" ref= "mongoDbFactory" />
bean >
beans >
3 mongo.properties
mongo.dbname=chy
mongo.host=127.0.0.1
mongo.port=27017
mongo.username=tester
mongo.password=123456
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1500
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive= true
mongo.socketTimeout=1500
mongo.slaveOk=true

你可能感兴趣的:(mongodb)