zoukankan      html  css  js  c++  java
  • 使用EmbeddedValueResolverAware读取配置文件内容

    在基于Spring获取properties文件属性值的时候,一般使用@Value的方式注入配置文件属性值,但是总是需要引入这些多余的变量,有点不爽,今天研究了下,基于Spring解析@Value的方式,使用EmbeddedValueResolverAware解析配置文件,实现起来也很简单

    工具类如下:

    @Component
    public class PropertiesUtil implements EmbeddedValueResolverAware {
    
        private StringValueResolver resolver;
    
        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            this.resolver = resolver;
        }
    
    
        /**
         * 获取属性,直接传入属性名称即可
         * @param key
         * @return
         */
        public String getPropertiesValue(String key) {
            StringBuilder name = new StringBuilder("${").append(key).append("}");
            return resolver.resolveStringValue(name.toString());
        }
    
    }

    使用:

    @Autowired
    private PropertiesUtil propertiesUtil;

    String s = propertiesUtil.getPropertiesValue("test.hello");

    不同于读取静态文件的方式,这种方式能加载类似于 test.hello = ${test.a}/${test.b}的属性值
  • 相关阅读:
    最短路-dij
    链式前向星
    B树、B+树
    C++类
    差分约束
    数位DP
    Markdown编辑器:表格
    git使用笔记
    leetcode 162. 寻找峰值(二分)
    python matplotlib包的安装注意事项:报错——No such file or dir : tmp/matplotlib-xxxxxx
  • 原文地址:https://www.cnblogs.com/winkey4986/p/7001173.html
Copyright © 2011-2022 走看看