在spring中配置mongodb的用户名和密码

1、在spring的配置文件中

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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" 

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

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/context     

        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:annotation-config />


<context:component-scan base-package="com.sy.gms" />

     

    <bean id="packagesToScan" class="org.springframework.beans.factory.config.ListFactoryBean">  

        <property name="sourceList">  

            <list>  

                <value>com.sy.gms.modelvalue>  

            list>  

        property>  

    bean>  


  

   <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">

  <constructor-arg   name="username" value="ebank" /> 

  <constructor-arg   name="password" value="ebank" /> 

   bean>


<import resource="mongodb-context.xml" />

beans>


在配置文件中配置一个org.springframework.data.authentication.UserCredentials的bean,把这个bean引入到mongoldb的配置文件中

   

   <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">

  <constructor-arg   name="username" value="ebank" /> 

  <constructor-arg   name="password" value="ebank" /> 

   bean>


在mongodb的配置文件中:

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:mongo="http://www.springframework.org/schema/data/mongo"  

    xsi:schemaLocation="http://www.springframework.org/schema/context   

          http://www.springframework.org/schema/context/spring-context-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/beans   

          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

          

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

    

    <mongo:mongo id="mongo" replica-set="${mongo.hostport}">

       <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 dbname="database" mongo-ref="mongo"/>

    

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

        <constructor-arg ref="mongo" />

        <constructor-arg name="databaseName" value="ebank" />

        <constructor-arg ref="userCredentials"/>

    bean>

beans>


引入在spring配置的userCredentials 即可




你可能感兴趣的:(java,mongodb)