自制加载配置类的工具类

通常要把properties文件中的值给取出来

用springboot 的@value属性并不能很好的做到,只能在controller中赋给成员函数。
那么接下来自己做一个吧

。。。。。。

@Slf4j
public class PropertiesUtil {
    private static Properties properties;
    static {
        String fileName="application.properties";
        properties=new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName)));
        } catch (IOException e) {
            log.error("配置文件读取异常",e);
        }

    }

    public static String getPropertity(String key){
        String value=properties.getProperty(key.trim());
        if (StringUtils.isNotBlank(value)){
            return value.trim();
        }
        return null;
    }
    public static String getPropertity(String key,String defaultValue){
        String value=properties.getProperty(key.trim());
        if (!StringUtils.isNotBlank(value)){
            value=defaultValue;
        }
        return value.trim();
    }
}
发布了34 篇原创文章 · 获赞 0 · 访问量 590

猜你喜欢

转载自blog.csdn.net/weixin_44841849/article/details/105010922
今日推荐