java properties util_PropertiesUtil 类  读取配置文件java

packagecom.xyz.imageserver.common.properties;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.Properties;importjava.util.concurrent.ConcurrentHashMap;

public classPropertiesUtil {private Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);private ConcurrentHashMapproMap;privatePropertiesUtil() {

proMap= new ConcurrentHashMap();

}private static PropertiesUtil instance = newPropertiesUtil();

public staticPropertiesUtil getInstance()

{returninstance;

}

publicString getSysPro(String key){return getSysPro(key, null);

}

publicString getSysPro(String key,String defaultValue){return getValue("spring/imageserver-"+System.getProperty("spring.profiles.active")+".properties", key, defaultValue);

}

publicString getValue(String fileName,String key,String defaultValue){

String val= null;

Properties properties=proMap.get(fileName);if(properties == null){

InputStream inputStream= PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);try{

properties= newProperties();

properties.load(new InputStreamReader(inputStream,"UTF-8"));

proMap.put(fileName, properties);

val=properties.getProperty(key,defaultValue);

}catch(IOException e) {

logger.error("getValue",e);

}finally{try{if (inputStream != null) {

inputStream.close();

}

}catch(IOException e1) {

logger.error(e1.toString());

}

}

}else{

val=properties.getProperty(key,defaultValue);

}returnval;

}

}


版权声明:本文为weixin_39902870原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。