<?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.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
<!– 加载mongodb的属性配置文件 –>
<context:property-placeholder location=”classpath:*.properties” />
<!– 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 –>
<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”
username=”han”
password=”906016″
mongo-ref=”mongo”
/>
<!– <mongo:db-factory dbname=”database” mongo-ref=”mongo” />–>
<!– 默认Mongodb类型映射 –>
<bean id=”defaultMongoTypeMapper” class=”org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper”>
<constructor-arg name=”typeKey”>
<null /><!– 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 –>
</constructor-arg>
</bean>
<bean id=”mongoTemplate” class=”org.springframework.data.mongodb.core.MongoTemplate”>
<constructor-arg ref=”mongo” />
<constructor-arg name=”databaseName” value=”duola” />
</bean>
</beans>
但是这种配置是没有权限的访问,一但加入权限 用户名和密码 我们该怎么配置才能连接上去?
10
<mongo:db-factory id=”dbFactory” dbname=”数据库名” mongo-ref=”mongo”
username=”username” password=”password”/>
<bean id=”mongoTemplate” class=”org.springframework.data.mongodb.core.MongoTemplate”>
<constructor-arg ref=”dbFactory”/>
</bean>
本人这样的配置就可以通过权限验证 后台测试没问题 题主可以试试
30
<mongo:mongo-client id="mongo" host="192.168.3.98" port="27017" credentials="用户名:密码@拥有权限的数据库"> <mongo:mongo-client>