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);
原文地址:https://www.cnblogs.com/xunianchong/p/6650677.html