sentinel之自定义加载配置工具

前言:

有关于在项目中加载配置或者加载其他文件,具体使用哪种方式加载笔者一直搞不清楚,有的直接用File就能搞定,有的需要用ClassLoader.getResources()方法,还有的是相对路径,确实有点太难为人了。

在学习Sentinel的源码时,偶然发现了其关于加载配置的一个工具类,刚好满足了笔者的需求,就拿来记录下,以后有类似需求的话可以直接上代码。

1.Sentinel之ConfigUtil

话不多少,直接上代码

public final class ConfigUtil {
    public static final String CLASSPATH_FILE_FLAG = "classpath:";
    // 方法只有这一个,给定一个file的路径即可
    public static Properties loadProperties(String fileName) {
        if (StringUtil.isNotBlank(fileName)) {
            // 如果是绝对路径
            if (absolutePathStart(fileName)) {
                return loadPropertiesFromAbsoluteFile(fileName);
            // 如果路径以classpath:开头    
            } else if (fileName.startsWith(CLASSPATH_FILE_FLAG)) {
                return loadPropertiesFromClasspathFile(fileName);
            } else {
                // 其他的情况默认是相对路径
                return loadPropertiesFromRelativeFile(fileName);
            }
        } else {
            return null;
        }
    }
}

1.1 loadPropertiesFromAbsoluteFile() 加载绝对路径配置

private static Properties loadPropertiesFromAbsoluteFile(String fileName) {
        Properties properties = null;
        try {

            // 既然是绝对路径,那么直接使用File加载的方式即可
            File file = new File(fileName);
            if (!file.exists()) {
                return null;
            }

            try (BufferedReader bufferedReader =
                         new BufferedReader(new InputStreamReader(new FileInputStream(file), getCharset()))) {
                properties = new Properties();
                properties.load(bufferedReader);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return properties;
    }

// 判断绝对路径的方式也很简单粗暴,就是查看文件路径是否以当前机器根目录开头
private static boolean absolutePathStart(String path) {
    File[] files = File.listRoots();
    for (File file : files) {
        if (path.startsWith(file.getPath())) {
            return true;
        }
    }
    return false;
}

1.2 loadPropertiesFromClasspathFile() 加载classpath下的文件

private static Properties loadPropertiesFromClasspathFile(String fileName) {
    fileName = fileName.substring(CLASSPATH_FILE_FLAG.length()).trim();

    List<URL> list = new ArrayList<>();
    try {
        // 使用ClassLoader加载的方式
        Enumeration<URL> urls = getClassLoader().getResources(fileName);
        list = new ArrayList<>();
        while (urls.hasMoreElements()) {
            list.add(urls.nextElement());
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    if (list.isEmpty()) {
        return null;
    }

    Properties properties = new Properties();
    for (URL url : list) {
        try (BufferedReader bufferedReader =
             new BufferedReader(new InputStreamReader(url.openStream(), getCharset()))) {
            Properties p = new Properties();
            p.load(bufferedReader);
            properties.putAll(p);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    return properties;
}

1.3 loadPropertiesFromRelativeFile() 加载相对路径文件

private static Properties loadPropertiesFromRelativeFile(String fileName) {
    String userDir = System.getProperty("user.dir");
    // 先将相对路径转变为绝对路径,然后再加载
    String realFilePath = addSeparator(userDir) + fileName;
    return loadPropertiesFromAbsoluteFile(realFilePath);
}

总结:

关于ConfigUtil的工具类,写的还是比较完整的,后续我们在项目中,可以直接拿来使用即可,避免在多处使用不用的工具类加载配置文件。

Guess you like

Origin blog.csdn.net/qq_26323323/article/details/121234775