2015-11-05 74 views
1

我在這裏有點困難。我正在設計一個實用程序,我需要我的Spring上下文將命令行參數作爲屬性來處理。這是很容易做到:使用PropertyPlaceholderConfigurer和PropertySource

if (args != null && args.length > 0) { 
    PropertySource<?> ps = new SimpleCommandLinePropertySource(args); 
    ctx.getEnvironment().getPropertySources().addFirst(ps); 
} 

我有什麼問題,是下一步:要符合我的企業框架,我必須設置PropertyPlaceholderConfigurer他們提供。也很容易完成。

@Bean 
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { 
    return new MyPropertyPlaceholderConfigurer(); 
} 

的事情是,一旦後者被設置,有什麼事先用命令行的工作也沒有了,我有錯誤:

java.lang.IllegalArgumentException異常:無法解析的佔位符「input.file」的字符串值「$ {} input.file」

現在,我知道使用這兩種很不理想(和理想,我甚至不應該使用PropertyPlaceholderConfigurer而是PropertySourcesPlaceholderConfigurer)。然而,我沒有選擇配置器。

因此,我認爲我必須改變一些東西與我的PropertySource,但我不知道如何以優雅的方式做到這一點。我是否應該擴大PropertyPlaceholderConfigurer以添加PropertySource?這甚至有可能嗎?

這種情況下最好的解決方案是什麼?即使模糊的線索也是受歡迎的,因爲我不知道要走哪條路。

(Spring版本:4.1.6)


1.本PropertyPlaceholderConfigurer負載一些屬性文件和應用一些額外的處理(例如,以允許在屬性文件中的加密值)。

回答

0

隨着

ctx.getEnvironment().getPropertySources().addFirst(ps); 

你走正確的道路。

我認爲您的MyPropertyPlaceholderConfigurer(來自該企業框架)不僅與PropertySourcesPlaceholderConfigurer兼容。

你應該看看他們的代碼並覆蓋它來擴展PropertySourcesPlaceholderConfigurer

從對方PlaceholderConfigurerSupportBeanFactoryPostProcessor和這些人必須被配置爲靜態@Bean秒。

HTH

+0

由於配置器的原因,addFirst似乎被忽略。我希望避免重寫框架代碼,但我認爲這是唯一的方法。遇到一些問題(看起來他們賣給了我夢想),但你的答案符合我的想法。 – Chop

相關問題