2015-11-05 29 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

相关问题