jyy
2021-01-26 ab2879bbcb846256cc182198b9c04e50fbc276c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.matrix.core.tools;
 
import java.io.IOException;
import java.util.Properties;
 
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
 
/**
 * 读取定义的属性文件
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2017年11月28日
 */
public class PropertiesUtil {
 
    private static final String SYSTEM_PROPERTIES_FILE = "config/core/system.properties";
    private static final String SYSTEM_PROPERTIES_FILE_BOOT = "config/system.properties";
 
    /**
     * 获取system.properties中定义信息
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2017年11月28日
     * @param key
     * @return
     */
    public static String getString(String key) {
        Properties prop = null;
        try {
            Resource resource = new ClassPathResource(SYSTEM_PROPERTIES_FILE);
            if (!resource.exists()) {
                resource = new ClassPathResource(SYSTEM_PROPERTIES_FILE_BOOT);
            }
            EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
            prop = PropertiesLoaderUtils.loadProperties(encodedResource);
        } catch (IOException e) {
            LogUtil.error(e.getMessage(), e);
        }
        if (prop != null) {
            return prop.getProperty(key);
        }
        return null;
    }
 
}