看了一下mongoTemplate 这个类发现有username和password两个属性
添加
<property name="username" value="mongodb"></property>
<property name="password" value="mongodb"></property>就可以了了
<property name="password" value="mongodb"></property>就可以了了
<constructor-arg name="username" value="mongodb"/> 不可以用另外的版本估计也是不行的
另外集合和实体类映射问题还不清楚,可以用insert(collectionName,person)替代下
?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">
<mongo:mongo host="192.168.1.1" port="27017"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="esb"/>
<property name="username" value="mongodb"></property>
<property name="password" value="mongodb"></property>
</bean>
<bean id="personRepository" class="com.mongo.dao.impl.PersonRepository">
<property name="mongoTemplate" ref="mongoTemplate"></property>
</bean>
<context:annotation-config />
</beans>
<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">
<mongo:mongo host="192.168.1.1" port="27017"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="esb"/>
<property name="username" value="mongodb"></property>
<property name="password" value="mongodb"></property>
</bean>
<bean id="personRepository" class="com.mongo.dao.impl.PersonRepository">
<property name="mongoTemplate" ref="mongoTemplate"></property>
</bean>
<context:annotation-config />
</beans>