1.读取property文件:
public static void main(String[] args) throws IOException {
// 第一中方法
Properties pro = PropertiesLoaderUtils
.loadAllProperties("property/test.properties");
System.out.println(pro.getProperty("name"));
// 第2中方法
Properties prop = PropertiesLoaderUtils.loadAllProperties(
"property/test.properties", ClassUtils.getDefaultClassLoader());
System.out.println(prop.getProperty("name"));
}
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}
第二种方法获取对应的classLoad;
2.property转map:
public static void main(String[] args) throws IOException {
Properties prop = PropertiesLoaderUtils.loadAllProperties(
"property/test.properties", ClassUtils.getDefaultClassLoader());
Map<String, Object> handlerMappings = new ConcurrentHashMap<String, Object>();
CollectionUtils.mergePropertiesIntoMap(prop, handlerMappings);
System.out.println(handlerMappings.get("name"));
}