配置读取基础类

 1、BaseReader为基础类,为方便后续新增reader类使用

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseReader {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseReader.class);

    private static Properties config = new Properties();

    private static final Map<String, Map<String, String>> CONFIG_TYPE_MAPPING = new HashMap<String, Map<String, String>>();

    protected BaseReader() {
    }

    protected static void readProperties(String[] filePaths, String keyword) {
        for (String filePath : filePaths) {
            InputStreamReader isr = null;
            try {
                InputStream resourceAsStream = BaseReader.class.getClassLoader().getResourceAsStream(filePath);
                if (resourceAsStream == null) {
                    LOGGER.info("{} 文件不存在, 忽略", filePath);
                    continue;
                }

                isr = new InputStreamReader(resourceAsStream, "UTF-8");
                config.load(isr);
            } catch (IOException e) {
                LOGGER.error("加载 {} 出错", filePath, e);
                continue;
            } finally {
                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        LOGGER.error("read {} config failed", filePath, e);
                        continue;
                    }
                }
            }
            Map<String, String> map = new HashMap<String, String>();
            for (Map.Entry<Object, Object> entry : config.entrySet()) {
                String keyName = entry.getKey().toString();
                String value = entry.getValue().toString();
                if (keyName.contains(keyword)) {
                    map.put(keyName, value);
                }
            }
            CONFIG_TYPE_MAPPING.put(keyword, map);
            LOGGER.info("{} 文件配置读取成功", filePath);
        }

    }

    /**
     * 
     * @Description:  根据key找到对应的map
     * @date  2017年8月11日 上午10:10:38
     * @param key
     * @return   List<String>
     */
    protected static Map<String, String> getMapping(String key) {
        Map<String, String> map = CONFIG_TYPE_MAPPING.get(key);
        return map != null ? map : new HashMap<String, String>();
    }
}

2、 SmrzfsReader为demo

public final class SmrzfsReader extends BaseReader {

    private static final String[] FILE_PATH = { "spring/is/is-smrz-mapping.properties", "override.properties" };

    private static final String KEYWORD = "smrz_convert_";

    static {
        readProperties(FILE_PATH, KEYWORD);
    }

    private SmrzfsReader() {
    }

    /**
     * 
     * @Description:  根据key找到value
     * @date  2017年8月11日 上午10:10:38
     * @param key
     * @return   List<String>
     */
    public static String getValue(String key) {
        return getMapping(KEYWORD).get(key);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值