美文网首页我爱编程
spring笔记-PropertyPlaceholderHelp

spring笔记-PropertyPlaceholderHelp

作者: 兴浩 | 来源:发表于2018-05-22 18:21 被阅读116次

    功能介绍:
    Property字符串占位符替换

    1.替换单个key

        private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
    
        @Test
        public void testWithProperties() {
            String text = "foo=${foo}";
            Properties props = new Properties();
            props.setProperty("foo", "bar");
    
            assertEquals("foo=bar", this.helper.replacePlaceholders(text, props));
        }
    

    2.替换多个key

        @Test
        public void testWithMultipleProperties() {
            String text = "foo=${foo},bar=${bar}";
            Properties props = new Properties();
            props.setProperty("foo", "bar");
            props.setProperty("bar", "baz");
    
            assertEquals("foo=bar,bar=baz", this.helper.replacePlaceholders(text, props));
        }
    

    3.替换递归属性

        @Test
        public void testRecurseInProperty() {
            String text = "foo=${bar}";
            Properties props = new Properties();
            props.setProperty("bar", "${baz}");
            props.setProperty("baz", "bar");
    
            assertEquals("foo=bar", this.helper.replacePlaceholders(text, props));
        }
    

    4.StringValueResolver

    定义了解析占位符的解析器策略,实际内部是使用了PropertyPlaceholderHelper实现

        private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
    
            private final PropertyPlaceholderHelper helper;
    
            private final PlaceholderResolver resolver;
    
            public PlaceholderResolvingStringValueResolver(Properties props) {
                this.helper = new PropertyPlaceholderHelper(
                        placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
                this.resolver = new PropertyPlaceholderConfigurerResolver(props);
            }
    
            @Override
            @Nullable
            public String resolveStringValue(String strVal) throws BeansException {
                String resolved = this.helper.replacePlaceholders(strVal, this.resolver);
                if (trimValues) {
                    resolved = resolved.trim();
                }
                return (resolved.equals(nullValue) ? null : resolved);
            }
        }
    

    5. PlaceholderResolver

    自定义占位符替换接口,比如以下在replace时则不通过Properties数据源,而是通过自定义的数据源来处理

        @Test
        public void testWithResolver() {
            String text = "foo=${foo}";
    
            assertEquals("foo=bar",
                    this.helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
                        @Override
                        public String resolvePlaceholder(String placeholderName) {
                            if ("foo".equals(placeholderName)) {
                                return "bar";
                            }
                            else {
                                return null;
                            }
                        }
                    }));
        }
    

    相关文章

      网友评论

        本文标题:spring笔记-PropertyPlaceholderHelp

        本文链接:https://www.haomeiwen.com/subject/ytfkjftx.html