我正在创建具有两个子模块的Maven多模块。一个是具有数据库存储库的简单DAO层,另一个是Spring Boot应用程序,它是命令行应用程序。我正在使用mongodb和spring-data-monodb作为数据库。当我使用xml config作为配置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
答案 0 :(得分:0)
CommandLineRunner为时已晚,无法在春季注册Bean定义,因此我们需要实现 BeanFactoryPostProcessor 来加载Bean定义xml,我没有完全测试以下代码,您可以做到给我反馈
CREATE EVENT SESSION [<MachineName>] ON SERVER
ADD EVENT sqlserver.rpc_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.sql_text)
WHERE ([sqlserver].[client_hostname]=N'<MachineName>')),
ADD EVENT sqlserver.sql_batch_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.sql_text)
WHERE ([sqlserver].[client_hostname]=N'<MachineName>')),
ADD EVENT sqlserver.sql_statement_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.sql_text)
WHERE ([sqlserver].[client_hostname]=N'<MachineName>'))
ADD TARGET package0.event_file(SET filename=N'C:\ExtEvents\<MachineName>.xel',max_file_size=(5120))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)
答案 1 :(得分:0)
您应该实现 BeanDefinitionRegistryPostProcessor ,这样就可以尽早注册Bean
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
@SpringBootApplication
public class Application implements BeanDefinitionRegistryPostProcessor {
private static String xmlFile;
public static void main(String[] args) throws Exception {
if (args.length == 0 ) {
System.out.println("Provide path for mongodb connection file");
return;
}
xmlFile = args[0];
SpringApplication.run(MongoApplication.class, args);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(registry);
Resource beanFile = new FileSystemResource(Application.xmlFile);
xmlBeanDefinitionReader.loadBeanDefinitions(beanFile);
}
}