我有一个 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 文件创建上下文,但这也不起作用。
我可以选择哪些选项将属性从命令行参数加载到我的应用程序中?
最佳答案
您是否尝试在添加属性源后进行刷新?
context.getEnvironment().getPropertySources().addFirst(clps);
context.refresh();
关于java - 如何在 Camel Spring 独立应用程序中从命令行参数设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60564633/