Spring整合MongoTemplate

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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="com.mongo" />
    <!-- 獲取配置資源 -->
    <context:property-placeholder location="classpath:mongodb-context-config.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>
    <!-- 設(shè)置使用的數(shù)據(jù)庫 名-->
    <mongo:db-factory dbname="test" mongo-ref="mongo"/>
    <!-- mongodb的模板 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
</beans>

由于公司用的是dubbo容器,所以需要使用代碼的方式實現(xiàn)(類似于springboot)

String host ="your host";
        Integer port = "your port";
        String username = "username";
        String defaultDataBaseName = "default database name";
        String password = "password";

        //address
        ServerAddress address = new ServerAddress(host, port);

        //client
        MongoCredential mongoCredential = MongoCredential.createCredential(username, defaultDataBaseName, password.toCharArray());
        LinkedList<MongoCredential> mongoCredentials = Lists.newLinkedList();
        mongoCredentials.add(mongoCredential);
        MongoClient client = new MongoClient(address, mongoCredentials);
        client.setWriteConcern(WriteConcern.SAFE);

        //factory
        SimpleMongoDbFactory factory = new SimpleMongoDbFactory(client, defaultDataBaseName);

        //template
        MongoTemplate template = new MongoTemplate(factory);

        mongoTemplate = template;

以上代碼只要在容器加載的時候,執(zhí)行一遍, 就可以實現(xiàn)了.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這兩天高考成績的陸續(xù)公布,也讓我小激動了一下,雖然我已經(jīng)是一名大四的老學(xué)長了,但我還有一年才能畢業(yè),因為我的...
    GabrielMorningS閱讀 258評論 0 1
  • 二貨哥給媳婦寫信:“媳婦,老板幾個月沒發(fā)薪,沒錢匯給你,匯100個吻吧!” 不久二貨媳婦回信:“吻收到。給娃的校長...
    老羅xt閱讀 312評論 1 1
  • 淅淅瀝瀝,淅淅瀝瀝,半夜時候,迎來了開春第一場真正意義上的春雨,因為在農(nóng)村,雨點打在屋檐瓦背...
    亞里士多德的大表哥閱讀 367評論 0 0
  • 開會、放假,中斷了幾天的治療繼續(xù)。正好讓鼻子修正恢復(fù)幾天。今天又是兩次。治療效果沒感覺好,繼續(xù)等。堅持就是勝利嗎
    王學(xué)峰shui閱讀 227評論 0 0

友情鏈接更多精彩內(nèi)容