【发布时间】:2020-03-06 13:01:54
【问题描述】:
我有一个 Camel Spring 独立应用程序:
public static void main(final String[] args) throws Exception{
Main main = new Main();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("camel-context.xml");
main.setApplicationContext(context);
main.run();
}
我有一些属性需要在配置路由时使用。这些属性将在启动应用程序时来自命令行参数。路线已定义:
public class MyRouteBuilder extends SpringRouteBuilder {
@Autowired
private Environment environment;
@Override
public void configure() {
Map<String, String> loadedValues = getValuesFromProperties( envrionment );
// route definition
}
}
如何让这些属性在配置方法中使用?在运行应用程序之前,我不知道属性名称或值是什么。我将拥有数千种可能的属性。
我已经尝试使用 CommandLinePropertySource 来获取它们,但是这些值是在调用 config() 方法之后设置的:
CommandLinePropertySource clps = new SimpleCommandLinePropertySource(args);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("camel-context.xml");
context.getEnvironment().getPropertySources().addFirst(clps);
main.setApplicationContext(context);
我也尝试过创建上下文,添加属性,然后使用我的 camel-context.xml 文件创建上下文,但这也不起作用。
有哪些选项可以将命令行参数中的属性加载到我的应用程序中?
【问题讨论】:
标签: java spring apache-camel spring-camel