簡體   English   中英

如何從 Camel Spring 獨立應用程序中的命令行參數設置屬性

[英]How do i set properties from command line args in Camel Spring standalone app

我有一個 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM