1妨退,加載配置文件
public class PropertiesLoader {
private static Logger logger = Logger.getLogger(PropertiesLoader.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties;
public PropertiesLoader(String... resourcesPaths) {
properties = loadProperties(resourcesPaths);
}
public Properties getProperties() {
return properties;
}
/**
* 取出Property妇萄,但以System的Property優(yōu)先,取不到返回空字符串.
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return "";
}
/**
* 取出String類型的Property,但以System的Property優(yōu)先,如果都為Null則拋出異常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
}
/**
* 取出String類型的Property咬荷,但以System的Property優(yōu)先.如果都為Null則返回Default值.
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
}
/**
* 取出Integer類型的Property冠句,但以System的Property優(yōu)先.如果都為Null或內(nèi)容錯誤則拋出異常.
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
}
/**
* 取出Integer類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值幸乒,如果內(nèi)容錯誤則拋出異常
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
}
/**
* 取出Double類型的Property懦底,但以System的Property優(yōu)先.如果都為Null或內(nèi)容錯誤則拋出異常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
}
/**
* 取出Double類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值罕扎,如果內(nèi)容錯誤則拋出異常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
}
/**
* 取出Boolean類型的Property聚唐,但以System的Property優(yōu)先.如果都為Null拋出異常,如果內(nèi)容不是true/false則返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
}
/**
* 取出Boolean類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值,如果內(nèi)容不為true/false則返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
}
/**
* 載入多個文件, 文件路徑使用Spring Resource格式.
*/
private Properties loadProperties(String... resourcesPaths) {
Properties props = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException ex) {
logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
} finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
2.獲取配置
public final class Global {
private Global() {
}
public static Global getInstance() {
return LazyHolder.GLOBAL;
}
private static class LazyHolder {
private static final Global GLOBAL = new Global();
}
/**
* 保存全局屬性值
*/
private static Map<String, String> map = Maps.newConcurrentMap();
public static final String DEFAULT_CHARTSET = "UTF-8";
/**
* 屬性文件加載對象
*/
private static PropertiesLoader loader = new PropertiesLoader("配置文件名稱");
/**
* 獲取配置
*/
public static String getConfig(String key) {
String value = map.get(key);
if (value == null) {
value = loader.getProperty(key);
map.put(key, value != null ? value : StringUtils.EMPTY);
}
return value;
}
}