PropertiesLoaderUtils

PropertiesLoaderUtils是org.springframework.core.io.support包下的工具


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取properties文件工具类
 **/
public class ReadPropertiesUtil {
    private final static Logger log = LoggerFactory.getLogger(ReadPropertiesUtil.class);
    //
    static String PATH = new StringBuffer(Thread.currentThread().getContextClassLoader().getResource("").getPath()).append("config/properties/").toString();
    static Map<String,Object> propertiesMap = new HashMap<>();

    public static<T> T get(String key,String propertiesName,Class<T> t){
        if (String.class.getName().equals(t.getName())) {
            return (T)getString(key, propertiesName);
        }else if(Integer.class.getName().equals(t.getName())){
            return (T)getInteger(key, propertiesName);
        }
        return null;
    }
    public static String getString(String key,String propertiesName){
        return (String)getValue(key, propertiesName);
    }
    public static Integer getInteger(String key,String propertiesName){
        return Integer.valueOf(getString(key, propertiesName));
    }
    /**
     * 获取值
     * @param key
     * @param propertiesName
     * @return
     */
    public static Object getValue(String key,String propertiesName){
        if(propertiesName != null && !propertiesName.endsWith(".properties")){
            propertiesName = new StringBuffer(propertiesName).append(".properties").toString();
        }
        //判断文件是否修改
        isModifyFile(propertiesName);
        Properties properties = (Properties) propertiesMap.get(propertiesName);
        Object value = properties.get(key);
        log.info("读取配置信息,key:{},value:{}",key,value);
        return value;
    }

    /**
     * 判断文件是否修改
     * @param propertiesName
     */
    public static void isModifyFile(String propertiesName){
        File file = new File(PATH+propertiesName);
        String timeKey = new StringBuffer(propertiesName).append("time").toString();
        //最后一次修改时间戳
        long lastTime = propertiesMap.get(timeKey)==null?0:(long) propertiesMap.get(timeKey);
        //上次修改时间戳
        long curTime = file.lastModified();
        if(lastTime != curTime){
            //读取配置文件
            readFile(file,propertiesName);
            log.info("读取配置信息,文件重新加载.file:{};上次修改时间戳:{},最后一次修改时间戳:{}",propertiesName,curTime,lastTime);
        }
    }

    /**
     * 读取配置文件信息
     * @param file
     * @param propertiesName
     */
    public static void readFile(File file,String propertiesName){
        try {
            Properties properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
            propertiesMap.put(propertiesName,properties);
            propertiesMap.put(new StringBuffer(propertiesName).append("time").toString(),file.lastModified());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值