是Mongo驱动程序中的这个错误,还是我做错了什么?

低于异常:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.mongodb.ServerAddress[]' for property 'replicaSetSeeds'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.mongodb.ServerAddress] for property 'replicaSetSeeds[0]': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:465)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:496)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1437)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    ... 14 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.mongodb.ServerAddress] for property 'replicaSetSeeds[0]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:128)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:487)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
    ... 20 more

下面是我的弹簧配置


 <bean id="defaultMongoTypeMapper"  class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
 </bean>

 <bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mappingContext" />
    <property name="typeMapper" ref="defaultMongoTypeMapper" />
 </bean>

 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    <property name="writeConcern">
        <util:constant static-field="com.mongodb.WriteConcern.SAFE" ></util:constant>
    </property>
 </bean>

使用的罐子/驱动器:
Spring Bean 3.2.0.RELEASE.jar
mongo-java-driver-2.11.3.jar

使用以下代码修复了该问题:
<bean id="mongoClient" class="com.mongodb.MongoClient">
    <constructor-arg>
        <list>
            <value>161.141.16.100:27017</value>
            <value>161.141.16.101:27017</value>
        </list>
    </constructor-arg>
</bean>

<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongoClient"/>

最佳答案

您可能正在使用

<property name="[variable]" ref="[bean]"/>

代替
<property name="[variable]" value="[bean]"/>

ref引用一个spring bean,值转换为字符串。

07-24 15:35