Spring Data MongoDB 使用多个db

This configuration example works:

<context:property-placeholder location="classpath:mongo.properties"/>

    <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}">
        <mongo:options
                connections-per-host="${mongo.connectionsPerHost}"
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                connect-timeout="${mongo.connectTimeout}"
                max-wait-time="${mongo.maxWaitTime}"
                auto-connect-retry="${mongo.autoConnectRetry}"
                socket-keep-alive="${mongo.socketKeepAlive}"
                socket-timeout="${mongo.socketTimeout}"
                slave-ok="${mongo.slaveOk}"
                write-number="1"
                write-timeout="0"
                write-fsync="true"/>
    </mongo:mongo>

    <mongo:db-factory id="mongoDbFactory1" mongo-ref="mongo" dbname="domain"/>

    <mongo:db-factory id="mongoDbFactory2" mongo-ref="mongo" dbname="storage"/>

    <mongo:db-factory id="mongoDbFactory3" mongo-ref="mongo" dbname="another"/>

    <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory2" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongoDbFactory1"/>
    </bean>

    <bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
        <constructor-arg ref="mongoDbFactory2" />
        <constructor-arg ref="converter" />
    </bean>

    <bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongoDbFactory3"/>
    </bean>

Spring Data MongoDB 使用多个db》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注