org.springframework.biz.utils.SpringPropertiesBundleUtils Maven / Gradle / Ivy
package org.springframework.biz.utils;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.Assert;
/*
* 多Properties缓存管理;每个Properties文件路径对应一个Properties对象
*/
public abstract class SpringPropertiesBundleUtils {
protected static Logger LOG = LoggerFactory.getLogger(SpringPropertiesBundleUtils.class);
protected static ConcurrentMap cachedProperties = new ConcurrentHashMap();
protected static String encoding = "UTF-8";
/*
*
* 根据location路径 获取 Properties;
* 注意:location 采用 spring 资源路径匹配解析器
*
* - 1、“classpath”: 用于加载类路径(包括jar包)中的一个且仅一个资源;对于多个匹配的也只返回一个,所以如果需要多个匹配的请考虑“classpath*:”前缀
*
- 2、“classpath*”: 用于加载类路径(包括jar包)中的所有匹配的资源。
*
- 3、或单一路径,如:"file:C:/test.dat"、"classpath:test.dat"、"WEB-INF/test.dat"
*
* @param location : 资源路径
* @return Properties 返回类型
*/
public static void initProperties(String location,String encoding) {
Assert.notNull(location, " location is null!");
try {
SpringPropertiesBundleUtils.encoding = encoding;
Resource[] resources = SpringResourceUtils.getResources(location);
for (Resource resource : resources) {
String path = resource.getFile().getPath();
cachedProperties.put(path, getProperties(resource,encoding));
}
} catch (Exception e) {
LOG.error("couldn't load properties !", e);
}
}
//---------------------------------------------------------------------
// Get methods for java.util.Properties
//---------------------------------------------------------------------
public static ConcurrentMap getProperties(){
return cachedProperties;
}
/*
*
* 根据location路径 获取 Properties;
* 注意:location 采用 spring 资源路径匹配解析器
*
* - 1、“classpath”: 用于加载类路径(包括jar包)中的一个且仅一个资源;对于多个匹配的也只返回一个,所以如果需要多个匹配的请考虑“classpath*:”前缀
*
- 2、“classpath*”: 用于加载类路径(包括jar包)中的所有匹配的资源。
*
- 3、或单一路径,如:"file:C:/test.dat"、"classpath:test.dat"、"WEB-INF/test.dat"
*
* @param location : 资源路径
* @return Properties 返回类型
*/
public static Properties getProperties(String location) {
Assert.notNull(location, " location is null!");
Properties mergedProperties = new Properties();
try {
Resource[] resources = SpringResourceUtils.getResources(location);
for (Resource resource : resources) {
mergedProperties.putAll(getCached(resource));
}
} catch (Exception e) {
LOG.error("couldn't load properties !", e);
}
return mergedProperties;
}
/*
* 加载 所有 匹配classpath*:**\\/*.properties的配置文件
* @return Properties 返回类型
*/
public static Properties getRootProperties(){
Properties rootProperties = new Properties();
try {
//classpath*:*.properties
Resource[] resources = SpringResourceUtils.getRootProperties();
for (Resource resource : resources) {
rootProperties.putAll(getCached(resource));
}
} catch (Exception e) {
LOG.error("couldn't load properties !", e);
}
return rootProperties;
}
/*
* 加载 所有 匹配classpath*:**\\/*.properties 配置文件
* @return Properties 返回类型
*/
public static Properties getAllProperties(){
Properties properties = new Properties();
try {
//classpath*:**/*.properties
Resource[] resources = SpringResourceUtils.getProperties();
for (Resource resource : resources) {
properties.putAll(getCached(resource));
}
} catch (Exception e) {
LOG.error("couldn't load properties !", e);
}
return properties;
}
private static Properties getProperties(Resource resource,String encoding){
Properties properties = null;
try {
properties = PropertiesLoaderUtils.loadProperties(new EncodedResource(resource,encoding));
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
public static String getProperty(String key){
return getProperty(key, null);
}
public static String getProperty(String key, String defaultValue) {
Assert.notNull(key, " key is null!");
//临时变量
Properties properties = null;
Object value = null;
//循环缓存key
for (String filepath : cachedProperties.keySet()) {
properties = cachedProperties.get(filepath);
if(properties != null && !properties.isEmpty()){
value = properties.getProperty(key,defaultValue);
break;
}
}
//取值
return value == null ? null : value.toString();
}
//---------------------------------------------------------------------
// Reload methods for java.util.Properties
//---------------------------------------------------------------------
public static void reloadProperties(String location) {
Assert.notNull(location, " location is null!");
try {
Resource[] resources = SpringResourceUtils.getResources(location);
for (Resource resource : resources) {
setCache(resource,encoding);
}
} catch (Exception e) {
LOG.error("couldn't reload properties !", e);
}
}
public static void reloadRootProperties(){
try {
//classpath*:*.properties
Resource[] resources = SpringResourceUtils.getRootProperties();
for (Resource resource : resources) {
setCache(resource,encoding);
}
} catch (Exception e) {
LOG.error("couldn't reload properties !", e);
}
}
public static void reloadAllProperties(){
try {
//classpath*:**/*.properties
Resource[] resources = SpringResourceUtils.getProperties();
for (Resource resource : resources) {
setCache(resource,encoding);
}
} catch (Exception e) {
LOG.error("couldn't reload properties !", e);
}
}
//---------------------------------------------------------------------
// set/remove methods for java.util.Properties
//---------------------------------------------------------------------
public static boolean setProperties(String location, Hashtable