Java ???resource????????
???????????? ???????[ 2014/6/18 10:52:33 ] ????????Java
????java ?л?? resource??????getResource()?????????????class????classloader??????? ??????????????getResource(string name)???name?????????class????getResource(string name)?????? name???????resolveName??String name?????д???? ?????????:
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
|
private String resolveName(String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class<?> c = this;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0?? index).replace('.'?? '/')
+"/"+name;
}
} else {
name = name.substring(1);
}
return name;
}
|
?????????????????????class.getResource(string name)?????????????class??classloader??getResource(string name)??????????????class????
classloader?????getResource(string name)????????????????.?????????????????:
Class.getResource("") ?????????Class??????????????????
Class.getResource("/") ???????classpath??λ??
getClassLoader().getResource("") ???????classpath??λ??
getClassLoader().getResource("/") ?????!!
|
????class.getResource(name)??????????????classloader??getResoure()?????????class.getResource(name)???????name??????????name??"/"?????????"/"?????·??_+????·???γ???·???????name????"/"?????????class????????·??+name?γ???·???? name ????classloader.getResource()????????????/???.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11