【问题标题】:How do i set properties from command line args in Camel Spring standalone app如何在 Camel Spring 独立应用程序中从命令行参数设置属性
【发布时间】: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


    【解决方案1】:

    您是否尝试在添加属性源后进行刷新?

    context.getEnvironment().getPropertySources().addFirst(clps);
    context.refresh();
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2019-09-24
      • 2015-09-21
      • 2021-03-27
      • 2012-05-24
      • 2022-09-27
      相关资源
      最近更新 更多