类加载器
2015-09-11 13:20
316 查看
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
}
else if (this.classLoader != null) {
is = this.classLoader.getResourceAsStream(this.path);
}
else {
is = ClassLoader.getSystemResourceAsStream(this.path);
}
1.this.clazz.getResourceAsStream(this.path)或this.classLoader.getResourceAsStream(this.path)原理:
首先获取加载该类的类加载器,然后调用getResourceAsStream 最终通过调用
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
public URL getResource(String name) {
URL url;
if (parent != null) {
url = parent.getResource(name);
} else {
url = getBootstrapResource(name);
}
if (url == null) {
url = findResource(name);
}
return url;
}
首先调用父类的加载器,通过递归调用,父类加载器不存在就调用jvm的根类加载器
2.ClassLoader.getSystemResourceAsStream(this.path) 原理:
public static InputStream getSystemResourceAsStream(String name) {
URL url = getSystemResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
public static URL getSystemResource(String name) {
ClassLoader system = getSystemClassLoader();
if (system == null) {
return getBootstrapResource(name);
}
return system.getResource(name);
}
直接调用系统类加载器,不存在的话就调用jvm的根类加载器
is = this.clazz.getResourceAsStream(this.path);
}
else if (this.classLoader != null) {
is = this.classLoader.getResourceAsStream(this.path);
}
else {
is = ClassLoader.getSystemResourceAsStream(this.path);
}
1.this.clazz.getResourceAsStream(this.path)或this.classLoader.getResourceAsStream(this.path)原理:
首先获取加载该类的类加载器,然后调用getResourceAsStream 最终通过调用
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
public URL getResource(String name) {
URL url;
if (parent != null) {
url = parent.getResource(name);
} else {
url = getBootstrapResource(name);
}
if (url == null) {
url = findResource(name);
}
return url;
}
首先调用父类的加载器,通过递归调用,父类加载器不存在就调用jvm的根类加载器
2.ClassLoader.getSystemResourceAsStream(this.path) 原理:
public static InputStream getSystemResourceAsStream(String name) {
URL url = getSystemResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
public static URL getSystemResource(String name) {
ClassLoader system = getSystemClassLoader();
if (system == null) {
return getBootstrapResource(name);
}
return system.getResource(name);
}
直接调用系统类加载器,不存在的话就调用jvm的根类加载器
相关文章推荐
- xml report error
- javascript的历史
- [Android]使用Kotlin+Anko开发Android(一)
- Rust语言之HelloWorld
- Rust语言之HelloWorld
- NSDateFormatter转换时间字符串时的时区问题
- #1063 : 缩地
- hdu 3635 Dragon Balls(并查集应用)
- JavaWeb——文件上传和下载
- 教你透彻了解红黑树
- ios开发者账号申请流程
- 哈希算法
- 递归罗列磁盘内容
- [LeetCode 231] Power of Two
- 分页显示的思考与实现
- Linux Shell 脚本中字符串的连接方法
- 一生必读的60部名著
- 自己看别人看了没用,UNICODE下连接数据库
- SSHconnection
- win7下cocos2dx开发环境搭建