package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
public class PropertyUtil extends PropertyPlaceholderConfigurer{
private final static Logger logger = ();
private static Map<String, String> mapProperties;
//该方法只被加载一次,然后会将properties文件中的值以key-value形式存入到mapProperties中。
private static void processProperties(Properties props) throws BeansException {
if (mapProperties == null) {
mapProperties = new HashMap<String, String>();
}
Iterator<Entry<Object, Object>> iterator = ().iterator();
while(()) {
<Object, Object> entry = (<Object, Object>) ();
String strKey = (String) ();
String strValue = (String)();
try {
strValue=new String(("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
();
}
(strKey, strValue);
}
}
public static String getContextProperty(String name) {
if (mapProperties == null || () == 0) {
//其中()是properties配置文件所在上级文件夹名,即“config”,()获取项目根路径,
//strConfPath是文件夹路径
String strConfPath = () + ;
// ("---------->path=" + () );
//其中()是properties文件后缀,即“properties”,获取文件名字
List<String> lsFilePath = (strConfPath, , false);
if (lsFilePath == null || () == 0) {
return "";
}
for (String strPath : lsFilePath) {
ClassPathResource resource = new ClassPathResource( + "/" + strPath);
Properties props = null;
try {
props = (resource);//读取properties文件中的信息
} catch (IOException e) {
// TODO Auto-generated catch block
//();
("getContextProperty exception:" + ());
}
processProperties(props);//将其放入mapProperties中。
}
}
return (String) (name);然后根据传入的key取出properties中的value值
}
//在使用的时候直接调用()方法即可。方法中需要传入properties文件中key,
//需要将、替换为真实的值,然后可以直接使用。
public static void main(String[] args) {
("---------->" + getContextProperty());
}
}