
com.xwc1125.common.util.properties.PropertiesUtils Maven / Gradle / Ivy
The newest version!
package com.xwc1125.common.util.properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @Description: 属性文件管理器, 可以获取Properties文件属性
* @Author: xwc1125
* @Date: 2021/1/5 10:17
* @Copyright Copyright@2021
*/
public class PropertiesUtils {
/**
* 实例
*/
static PropertiesUtils instance;
private Map propMap = new HashMap();
private String defProperties = "config.properties" ; // 默认的属性文件
public static PropertiesUtils getInstance() {
return instance;
}
PropertiesUtils() {
// 默认读取config.properties
loadProp(defProperties);
}
private Properties loadProp(String fileName) {
Properties props = propMap.get(fileName);
if (props == null) {
Resource resource = new ClassPathResource(fileName);
try {
props = PropertiesLoaderUtils.loadProperties(resource);
propMap.put(fileName, props);
} catch (IOException e) {
props = new Properties();
e.printStackTrace();
}
}
return props;
}
private String getValue(String key, String fileName) {
Properties prop = propMap.get(fileName);
if (prop == null) {
prop = loadProp(fileName);
}
return prop.getProperty(key);
}
/**
* 根据key获取属性值
*
* @param key
* @return
*/
public String get(String key) {
return getValue(key, defProperties);
}
/**
* 根据key和文件名获取值
*
* @param key
* @param fileName 文件名,必须放在classpath根目录下
* @return
*/
public String get(String key, String fileName) {
return getValue(key, fileName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy