无法从appContext.xml:credential类型转换在springboot中创建MongoClient bean

时间:2018-02-28 02:40:50

标签: java spring spring-boot spring-mongo spring-mongodb

我有一个使用MongoDB Spring的现有Java库。该库包含applcationContext.xml和config.properties文件,这些文件将其配置为连接到远程MongoDB实例并正确执行。

我试图在SpringBoot应用程序中使用这个库,使用完全相同的两个文件,但是SpringBoot似乎在连接MongoDB客户端时遇到了麻烦,例外情况如下。这是我的应用程序类/配置:

@SpringBootApplication
@ImportResource({"applicationContext.xml"})
public class MySpringBootApplication {

  public static void main(String[] args) {
      SpringApplication.run(MySpringBootApplication.class, args);
  }

}

以下是例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential[]' for property 'credentials'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential' for property 'credentials[0]': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at org.MyCompany.MySpringBootApplication.main(MySpringBootApplication.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential[]' for property 'credentials'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential' for property 'credentials[0]': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:617)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1577)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1536)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    ... 20 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential' for property 'credentials[0]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:143)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:503)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:216)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
    ... 26 more

applicationContext.xml具有以下mongoDB配置:

<context:property-placeholder
  location="classpath:config.properties" />
<mongo:mongo-client 
  id="mongo" 
  replica-set="${mongo.replica.set}" 
  credentials="${mongo.credentials}" />

config.properties具有mongo.credentials的值。

我已经使用了spring-boot-mongodb并添加了配置并成功连接,但是在该文件中配置了许多其他bean,我不知道如何在依赖于mongoClient的SpringBoot中连接。如果告诉SpringBoot正确创建MongoCredential,会更快(我希望)。

我是否需要实现StringToMongoCredentialConverter?或者我注定要将applicationContext.xml中的所有东西都拉到某个Java配置类?

使用spring-boot 1.5.4,spring-core 4.3.9,spring-data-mongo 1.9.2

1 个答案:

答案 0 :(得分:0)

根据Connecting to MongoDB 3.0 with Java Spring中的答案,最后用以下内容替换元素:

<!-- Seeds -->
<bean id="mongoSeedListID" class="java.util.ArrayList">
<constructor-arg>
    <list>
        <ref bean="mongoSeedlID" />
    </list>
</constructor-arg>
</bean>

<bean id="mongoSeedlID" class="com.mongodb.ServerAddress">
        <constructor-arg type="java.lang.String" name="host" value="DATABASE_HOST" />
        <constructor-arg type="int" name="port" value="DATABASE_PORT" />
</bean>

<!-- Credentials -->
<bean id="mongoCredentialListID" class="java.util.ArrayList">
<constructor-arg>
    <list>
        <ref bean="mongoCredentialID" />
    </list>
</constructor-arg>
</bean>

<bean id="mongoCredentialID" class="com.mongodb.MongoCredential">
        <constructor-arg name="mechanism" value = "#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}" />
        <constructor-arg type="java.lang.String" name="userName" value="DATABASE_USERNAME" />
        <constructor-arg type="java.lang.String" name="source" value="DATABASE_SOURCE" />
        <constructor-arg type="char[]" name="password" value="DB_USER_PASS" />
</bean>

<!-- MongoClient -->
<bean id="mongoClientID" class="com.mongodb.MongoClient">
        <constructor-arg ref="mongoSeedListID" />
        <constructor-arg ref="mongoCredentialID"  />
</bean>

它有效!