无法将类型“ java.lang.String”的值转换为所需的类型“ com.mongodb.ReadPreference”

时间:2019-02-12 04:41:55

标签: spring spring-boot spring-data-mongodb spring-bean mongo-java-driver

我一直在与mongo进行尝试,尝试配置以下Bean:

 <bean id="someMongoOptions" class="SomeClassThatExtendFromMongoClientOptionsFactoryBean" scope="singleton">
    <!--assume here other properties are injected, but just readPreference is the relevant one failing-->
    <property name="readPreference" value="${some_value_here_for_this:PRIMARY}" />
</bean>

我还以这种方式使用CustomerEditorConfigurer

<bean id="myTestPropertyEditorRegistrar" class="org.springframework.beans.factory.config.CustomEditorConfigurer" scope="singleton">
    <property name="propertyEditorRegistrars">
        <list>
            <bean class="myClass.MyTestPropertyEditorRegistrar" />
        </list>
    </property>
</bean>



public class MyTestPropertyEditorRegistrar implements PropertyEditorRegistrar {

@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(ServerAddress[].class, new ServerAddressPropertyEditor());
    registry.registerCustomEditor(MongoCredential[].class, new MongoCredentialPropertyEditor());
    registry.registerCustomEditor(ReadPreference.class, new ReadPreferencePropertyEditor());
    registry.registerCustomEditor(WriteConcern.class, new WriteConcernPropertyEditor());
}

}

我正在使用: mongo-java-driver 3.5.0 spring-data-mongodb 2.0.5.RELEASE

根据我的理解,值应该没问题,应该没什么大不了的。

这是相关的错误:

  

无法将类型'java.lang.String'的属性值转换为   属性所需的类型'com.mongodb.ReadPreference'   'readPreference';嵌套的异常是java.lang.IllegalStateException:   无法将类型“ java.lang.String”的值转换为必需的类型   属性“ readPreference”的“ com.mongodb.ReadPreference”:否   找到匹配的编辑器或转换策略

这部分也可能与我在TRACE级别中进行部署有关,以查看创建bean时可能发生的事情:

  

DEBUG 36213 --- [main] org.springframework.beans.BeanUtils   :找不到类型的属性编辑器[com.mongodb.ReadPreferenceEditor]   com.mongodb.ReadPreference根据“编辑器”后缀约定   跟踪36213 --- [main] o.s.beans.TypeConverterDelegate
  :在[com.mongodb.ReadPreference]类型上找不到String构造函数

     

java.lang.NoSuchMethodException:   com.mongodb.ReadPreference。(java.lang.String)在   java.lang.Class.getConstructor0(Class.java:3082)〜[na:1.8.0_192]在   java.lang.Class.getConstructor(Class.java:1825)〜[na:1.8.0_192]在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:234)   〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在   org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585)   [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在   org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:604)   [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在   org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219)   [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

0 个答案:

没有答案