首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertySourcesPlaceholderConfigurer的动态定位

PropertySourcesPlaceholderConfigurer的动态定位
EN

Stack Overflow用户
提问于 2013-03-01 22:40:31
回答 1查看 769关注 0票数 0

我为占位符提供了基于注释的bean配置。在这个占位符的帮助下,我可以很容易地使用我想要的属性值。

代码语言:javascript
复制
@Bean
    public static PropertySourcesPlaceholderConfigurer initPlaceholder() {
        PropertySourcesPlaceholderConfigurer placeholder = new PropertySourcesPlaceholderConfigurer();
        placeholder.setLocation(new ClassPathResource("some.properties"));
        placeholder.setIgnoreUnresolvablePlaceholders(true);

        return placeholder;
    }

如何使用${some.properties}动态值设置此占位符?

代码语言:javascript
复制
placeholder.setLocation(new ClassPathResource(ANY_PROPERTIES));

我不能使用initPlaceholder(字符串属性)...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-01 23:39:05

我对此所做的是创建我自己的PropertyPlaceHolder (以获取外部属性文件)

代码语言:javascript
复制
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

public static final String ANY_PROPERTY = "ANY_PROPERTY";

private static final Log LOG = LogFactory.getLog(MyPropertyPlaceholderConfigurer.class);

@Override
protected void loadProperties(Properties props) throws IOException {

    String anyProperty = System.getProperty(ANY_PROPERTY);

    if (StringUtils.isEmpty(anyProperty)) {
        LOG.info("Using default configuration");
        super.loadProperties(props);

    } else {
        LOG.info("Setting HDFS LOCATION PATH TO : " + anyProperty);

        try {
            Path pt = new Path(anyProperty);
            Configuration conf = new Configuration();
            conf.set(FileSystem.FS_DEFAULT_NAME_KEY, anyProperty);
            FileSystem fs = FileSystem.get(conf);
            FSDataInputStream fileOpen = fs.open(pt);
            BufferedReader br = new BufferedReader(new InputStreamReader(fileOpen));
            props.load(br);
        } catch (Exception e) {
            LOG.error(e);
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15160344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档