1

为了学习(spring/mongoBD/CLoudBees),我在 CloudBees 上创建了一个 mongoDB 实例,我正在尝试使用 spring Data 连接我的 spring 应用程序。

我收到以下错误:

Caused by: org.springframework.data.mongodb.CannotGetMongoDbConnectionException: 
Failed to authenticate to database [appDB], username = [MY_DB_USERNAME], password = [b**********1]
at org.springframework.data.mongodb.core.MongoDbUtils.doGetDB(MongoDbUtils.java:114)
at org.springframework.data.mongodb.core.MongoDbUtils.getDB(MongoDbUtils.java:74)
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.getDb(SimpleMongoDbFactory.java:118)
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.getDb(SimpleMongoDbFactory.java:107)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.ensureIndex(MongoPersistentEntityIndexCreator.java:213)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator$1.doWithPersistentProperty(MongoPersistentEntityIndexCreator.java:146)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator$1.doWithPersistentProperty(MongoPersistentEntityIndexCreator.java:120)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:120)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:92)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:49)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:268)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:168)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:137)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:63)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:141)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:83)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 46 more

我的 Spring Data 的 XML 配置:

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="databaseName" value="appDB"/>
    <constructor-arg name="userCredentials" ref="mongoCredentials"/>
</bean>

<bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
     <constructor-arg name="username" value="#{mongoUri.username}" />
    <constructor-arg name="password" value="#{new java.lang.String(mongoUri.password)}" />
</bean>

<bean id="mongoUri" class="com.mongodb.MongoURI">
  <constructor-arg value="mongodb://MY_DB_USERNAME:MY_DB_PASS@paulo.mongohq.com:10048/xxxxxxxxxxxxx"/>
</bean>

<bean class="com.mongodb.Mongo" id="mongo">
    <constructor-arg ref="mongoUri" />
</bean>

当我使用 umongo 测试我的 URI mongodb://MY_DB_USERNAME:MY_DB_PASS@paulo.mongohq.com:10048/xxxxxxxxxxxxx 时,它连接正确。

Maven依赖:

spring data mongodb 版本:1.3.1.RELEASE
mongodb驱动版本:2.10.1
弹簧芯版本:3.2.3.RELEASE

知道我做错了什么吗?

4

1 回答 1

2

我通过用#{mongoUri.database} 替换我的数据库名称“appDB”解决了这个问题,所以 mongoTemplate 的新 Bean def 是:

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="databaseName" value="#{mongoUri.database}"/>
    <constructor-arg name="userCredentials" ref="mongoCredentials"/>
</bean>

我不明白为什么将数据库名称直接作为字符串是错误的,但无论如何对我来说它适用于这个 XML 配置。

于 2013-11-01T12:42:42.597 回答