????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()????????????/???.