spring中PropertyPlaceholderHelper替换占位符的值

1.Properties中的值替换¥{}或者#{}占位符

    String text = "foo=${foo},bar=${bar}";
        Properties props = new Properties();
        props.setProperty("foo", "foo_value");
        props.setProperty("bar", "bar_value");
        PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
        System.out.println( helper.replacePlaceholders(text, props));

2.hashmap或者对象替换¥{}或者#{}占位符

final Map<String,String> params = new HashMap<>();
        params.put("foo", "bar");
        params.put("bar", "bar");
        PropertyPlaceholderHelper helper1 = new PropertyPlaceholderHelper("${", "}", null, false);
        String s = helper1.replacePlaceholders(text, new PlaceholderResolver() {
            @Override
            public String resolvePlaceholder(String placeholderName) {
                // TODO Auto-generated method stub
                return params.get(placeholderName);
            }
        });
        System.out.println(s);

 

posted on 2017-03-31 12:07  xunianchong  阅读(477)  评论(0编辑  收藏  举报