1、getResource 与 getResources
加载类路径资源文件,Java通过类加载器提供便捷的方法,分别介绍
public static void demo () throws IOException{
// 当前类加载器
ClassLoader loader = ();
// getResource与getResources 都是【加载当前类加载器以及父类加载器所在路径的资源文件】
/**
*
* 加载当前类加载器以及父类加载器所在路径的资源文件
* 将遇到的第一个资源文件直接返回!!!
* 比如当前工程类路径有conf/文件,引入的第三方jar包也有这个文件
* 返回的是当前工程下的这个资源文件
*/
URL url = ("conf/");
/**
*
* 加载当前类加载器以及父类加载器所在路径的资源文件
* 将遇到的所有资源文件全部返回!
* 比如当前工程类路径有conf/文件,引入的第三方jar包也有这个文件
* 则将这些文件全部返回
*/
Enumeration<URL> enumeration = ("conf/");
// 打印出所有同名的资源文件
while (()) {
URL url1 = ();
("file=" + ());
}
// 3.直接将资源转化成流InputStream
InputStream in1 = ("conf/");
(in1 == null);
// 4.也可以根据获取的URL直接打开流
InputStream in2 = ();
(in2 == null);
}
2、源代码
/**
* getResource源代码:递归调用的
*/
public URL getResource(String name) {
URL url;
// 初始parent 肯定是 ExtClassLoader,且不为空
// 因为当前类加载器就是系统类加载器,即 AppClassLoader
// 他的父亲就是 ExtClassLoader
if (parent != null) {
// 递归调用:一旦再次进入递归方法,parent肯定就变成BootClassLoader
// 且一定为null,因为引导类加载器是C语言的native本地方法实现,在Java环境获取不到
url = (name);
} else {
url = getBootstrapResource(name);
}
if (url == null) {
url = findResource(name);
}
return url;
}
/**
* getResources 的源代码:也是递归调用
*/
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration[] tmp = new Enumeration[2];
if (parent != null) {
tmp[0] = (name);
} else {
tmp[0] = getBootstrapResources(name);
}
tmp[1] = findResources(name);
return new CompoundEnumeration<>(tmp);
}
3、Spring配置文件中常出现的 classpath 与 classpath*
这两者的区别,本质上就是 getResource 和 getResources 的区别,也有网友总结如下
(1)classpath和classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
(2)classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
(3)文件名包含通配符(如spring-*.xml,spring*.xml), 如果根目录为"",classpath加载不到任何资源, 而classpath*则可以
(4)classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源
4、读取Properties资源文件的工具类
明白了 getResource 和 getResources 的原理,再来读取各种资源文件就比较得心应手
此处使用了弱引用的WeakHashMap作为缓存,一定程度上避免重复加载资源文件
package ;
import ;
import ;
import ;
import ;
import ;
/**
* 读取 properties 资源文件
*
* @author yli
*/
public class PropertiesUtil {
private static ClassLoader loader = ();
private static WeakHashMap<String, Properties> cache = new WeakHashMap<String, Properties>();
private static Logger log = (());
public static Properties getProperties(String file) {
Properties p = (file);
if (null != p) {
return p;
}
try {
InputStream inStream = (file);
p = new Properties();
(inStream);
(file, p);
} catch (IOException e) {
(());
}
return p;
}
public static String get(String file, String key) {
Properties p = getProperties(file);
if (null == p) {
("**** 读取资源文件失败 ****");
return null;
} else {
return (key);
}
}
}