您好我正在使用Spring MVC和MongoDB。 我想知道在哪里我必须提到参数“Authsource = admin”。 同时我无法在mongoTemplate bean中提供mongoCredentials,它抛出bean创建异常。
下面是我的dispatcher-servlet.xml
<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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="54.172.243.152" />
<property name="port" value="27005" />
</bean>
<bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="smynapati" />
<constructor-arg name="password" value="smynapati" />
<!-- <constructor-arg name="authSource" value="admin" /> -->
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<!-- <constructor-arg name="mongoCredentials" ref="mongoCredentials" /> -->
<constructor-arg name="databaseName" value="test1" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<context:component-scan base-package="com.userlogin"/>
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
答案 0 :(得分:1)
假设您正在使用1.5.2.RELEASE
的版本spring-data-mongodb
(我记得您之前的问题)并且凭据存储在admin
数据库中,这些应该是您的MongoDB相关bean设置:
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="54.172.243.152" />
<property name="port" value="27005" />
</bean>
<bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="smynapati" />
<constructor-arg name="password" value="smynapati" />
</bean>
这些保持不变。接下来:
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="test1" />
<constructor-arg name="credentials" ref="mongoCredentials" />
<constructor-arg name="authenticationDatabaseName" value="admin" />
</bean>
最后是模板:
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
请注意,为了简单起见,您最好使用mongo
XML命名空间。