【发布时间】:2019-01-23 14:17:46
【问题描述】:
我正在创建具有两个子模块的 Maven 多模块。一个是具有数据库存储库的简单 DAO 层,另一个是 Spring Boot 应用程序,它是命令行应用程序。我正在使用 mongodb 和 spring-data-monodb 作为数据库。当我使用 xml config as 配置 mongodb 时
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoClient" />
<constructor-arg name="databaseName" value="test" />
</bean>
<mongo:mongo-client id="mongoClient"
credentials="username:password@source" replica-set="server1uri:27017,server2uri:27017,server3uri:27017" >
<mongo:client-options connections-per-host="50" threads-allowed-to-block-for-connection-multiplier="5000" ssl="true" />
</mongo:mongo-client>
</beans>
它工作正常。我已经在 dao 模块中定义了这个文件,并在 SpringBootApplication 上使用 @ImportResource 在 Spring Boot 模块中使用它,它工作正常。
现在我需要在运行 spring boot jar (java -jar app.jar /path/to/bean.xml) 时将此文件作为命令行参数提供。我所做的是,我在 SpringBootApplication 中实现了 CommandLineRunner,并在该方法中使用 XmlBeanDefinitionReader 加载上述 xml 文件。
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private GenericApplicationContext applicationContext;
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
if (args.length == 0 ) {
System.out.println("Provide path for mongodb connection file");
return;
}
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
Resource beanFile = new FileSystemResource(args[0]);
xmlBeanDefinitionReader.loadBeanDefinitions(beanFile);
// Rest of logic.
}
}
然后它不能正确地投射 mongo 凭据。它抛出以下错误。
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
不知何故,它无法转换凭据。我正在使用 spring boot 2.0.2.RELEASE
【问题讨论】:
标签: java spring spring-boot