org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘mongo-client’: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.mongodb.MongoCredential[]] for property ‘credentials’; nested exception is java.lang.IllegalArgumentException: Credentials need to specify username!
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.mongodb.MongoCredential[]] for property ‘credentials’; nested exception is java.lang.IllegalArgumentException: Credentials need to specify username!
完整的mongoDb配置信息如下,credentials包括三部分,用户名,密码,数据库名称,如果少写了@${mongo.dbname}就会报以上的错误信息
<mongo:mongo-client id="mongo-client" host="${mongo.host}" port="${mongo.port}"
credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
<mongo:client-options write-concern="SAFE"/> </mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongo-client" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean>