博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动扫描Properties文件配置的简单实现
阅读量:7222 次
发布时间:2019-06-29

本文共 3892 字,大约阅读时间需要 12 分钟。

hot3.png

最近搞一个简单的Demo项目的时候,需要读取Properties文件中的配置信息,不想一个个文件写代码读取,也不想引入其它庞大的框架来进行自动扫描读取,就自己写了一个简单的自动扫描class目录下所有properties文件中的配置信息的工具。


项目结构如下图:

JDK版本为1.8.

image

配置文件上下文(PropertiesContext.class):

PropertiesContext类是一个单例,持有所有读取出来的配置信息,通过该对象的方法拿到所需要的配置信息。

package org.cent.tools.properties_reader.context;import org.cent.tools.properties_reader.util.PropertiesReader;import java.util.Properties;/** * 配置信息上下文 * Created by cent on 2016/10/24. */public enum PropertiesContext {    /**     * 枚举实现单例模式     */    INSTANCE;    /**     * 读取到的配置信息(内存)     */    private Properties properties;    /**     * 初始化读取配置     */    {        PropertiesReader reader=new PropertiesReader();        properties= reader.readAllProperties();    }    /**     * 根据key获取配置值     * @param key     * @return     */    public String getProperty(String key) {        return (String) properties.get(key);    }    /**     * 根据key获取配置值,如果配置值为null,则返回默认值     * @param key     * @param defaultVal 默认值     * @return     */    public String getPropertyOrDefault(String key, String defaultVal) {        return properties.get(key) == null ? (String) properties.get(key) : defaultVal;    }    /**     * 是否包含该key的配置     * @param key     * @return     */    public boolean containsKey(String key){        return properties.containsKey(key);    }}

配置文件读取者(PropertiesReader.class)

该类封装了配置文件读取的入口方法,其中:readAllProperties()方法为读取class目录下所有配置文件;readProperties(File path)为读取指定路径下的所有配置文件。

package org.cent.tools.properties_reader.util;import java.io.File;import java.util.Properties;/** * Created by cent on 2016/10/24. */public class PropertiesReader {    /**     * 读取所有目录配置文件     * @return     */    public Properties readAllProperties(){        //获取class根目录        File root=new File(this.getClass().getClassLoader().getResource("").getFile());        return readProperties(root);    }    /**     * 读取指定目录配置文件     * @param path 指定目录     * @return     */    public Properties readProperties(File path){        return PropertiesFileUtil.recursiveFile(path, PropertiesFileUtil::checkAndReadProperties);    }}

配置文件读取工具类(PropertiesFileUtil.class)

递归读取文件目录下的Properties文件,并存储到Properties对象中返回。

package org.cent.tools.properties_reader.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.Arrays;import java.util.Properties;import java.util.function.Function;/** * 配置文件工具类 * Created by cent on 2016/10/24. */public abstract class PropertiesFileUtil {    /**     * 配置文件后缀名常量     */    private static final String PROPERTIES_SUFFIX=".properties";    /**     * 递归各个文件夹读取配置文件,如果是文件执行回调函数     * @param root 根目录     * @param isFileThenDoFunction 如果是文件,则执行该回调函数     * @return     */    public static Properties recursiveFile(File root, Function
isFileThenDoFunction){ Properties properties=new Properties(); if(root.exists()) { File[] files = root.listFiles(); //并发流读取properties文件 Arrays.asList(files).parallelStream().forEach(file -> { if(file.isFile()){ properties.putAll(isFileThenDoFunction.apply(file)); }else{ properties.putAll(recursiveFile(file,isFileThenDoFunction)); } }); } return properties; } /** * 判断是否配置文件,是则读取出来并返回 * @param file 需读取的文件 * @return */ public static Properties checkAndReadProperties(File file){ String suffix=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length()); if(PROPERTIES_SUFFIX.equals(suffix)){ Properties properties=new Properties(); try { properties.load(new FileInputStream(file)); return properties; } catch (IOException e) { e.printStackTrace(); } } return new Properties(); }}

示例源码地址

阿里Code源码地址:

转载于:https://my.oschina.net/centychen/blog/777438

你可能感兴趣的文章
python:浅析python 中__name__ = '__main__' 的作用
查看>>
修改tomcat端口后不能IP访问问题
查看>>
review board
查看>>
URAL 1495 One-two, One-two 2
查看>>
牛客国庆集训派对Day3 G Stones
查看>>
虚函数简单总结
查看>>
插入排序--算法导论
查看>>
NoSQL -- Redis使用
查看>>
处理iphone的 .play() 不能播放问题
查看>>
jetty404web界面服务器信息隐藏
查看>>
22个Photoshop网页设计教程网站推荐
查看>>
如何让程序员更容易的开发Web界面?重构SmartAdmin展示TinyUI
查看>>
centos7 python2和python3共存
查看>>
rhel6.2配置在线yum源
查看>>
分级聚类算法
查看>>
Web Services 入门(之二)
查看>>
随机模拟MCMC和Gibbs Sampling
查看>>
网络安全是一种态度
查看>>
POJ1131 Octal Fractions
查看>>
mysql-ulogd2.sql
查看>>