First look at a piece of code, the following generation is the code is to load resources from the CLASSPATH
Defaultresourceloader Resourceloader = new Defaultresourceloader ();
Resource Resource = Resourceloader.getresource ("classpath:test/a/");
URL url = resource.geturl ();
..//Read files based on URL
Org.springframework.core.io.DefaultResourceLoader.getResource (String) source code is as follows:
Public Resource getresource (String location) {
Assert.notnull (location, "location must is null");
for (Protocolresolver protocolResolver:this.protocolResolvers) {
Resource Resource = Protocolresolver.resolve ( location, this);
if (resource!= null) {return
resource;
}
}
if (Location.startswith ("/")) {return
Getresourcebypath (location);
}
else if (Location.startswith (Classpath_url_prefix)) {return
new Classpathresource location.substring ( Classpath_url_prefix.length ()), getClassLoader ());
else {
try {
//try to parse the location as a URL ...
URL url = new URL (location);
return new Urlresource (URL);
catch (Malformedurlexception ex) {
//No URL-> Resolve as resource path.
return Getresourcebypath (location);}}
The red tag in the code is the process of loading resources from Classpath, where you create a Classpathresource instance, the second parameter is to set a ClassLoader, and then look at Org.springframework.core.io.DefaultResourceLoader.getClassLoader method: @Override public ClassLoader getClassLoader () { return (This.classloader!= null this.classLoader:ClassUtils.getDefaultClassLoader ()); We did not assign a value to This.classloader, default to NULL, and continue to look at the Classutils.getdefaultclassloader () method:
public static ClassLoader Getdefaultclassloader () {
ClassLoader cl = null;
try {
cl = Thread.CurrentThread (). Getcontextclassloader ();
}
catch (Throwable ex) {
//cannot access thread classloader-falling back ...
}
if (CL = null) {
//No Thread Context class loader-> the Use class loader the This class.
CL = ClassUtils.class.getClassLoader ();
if (CL = null) {
//getClassLoader () Returning NULL indicates the bootstrap ClassLoader
try {
cl = Classlo Ader.getsystemclassloader ();
}
catch (Throwable ex) {
//Cannot access system Classloader-oh OK, maybe the caller can live with null ...
}
}
}
return cl;
}
At the red mark, the ClassLoader used by Classpathresource is shown as: Thread.CurrentThread (). Getcontextclassloader (); Contextclassloader of the current thread, this is related to which container (or environment) the current program is running in, such as running in the Tomcat8.5.6 container, Contextclassloader is: Org.apache.catalina.loader.ParallelWebappClassLoader
Looking back, Org.springframework.core.io.ClassPathResource.getURL () method: Public URL GetURL () throws ioexception {URL url = Reso Lveurl (); if (url = = null) {throw new FileNotFoundException (getdescription () + "cannot is resolved to URL because it does not exis T "); } return URL; Continue looking, Org.springframework.core.io.ClassPathResource.resolveURL (): Protected URL ResolveUrl () {if (This.clazz!= null) { Return This.clazz.getResource (This.path); else if (This.classloader!= null) {return this.classLoader.getResource (This.path);} else {return classloader.getsyst Emresource (This.path); If the Clazz call Clazz.getresource is set in the method implementation, if the ClassLoader call Classloader.getresource is set, otherwise use the Classloader.getsystemresource or as we run in Tomcat8.5.6 for example, we call Org.apache.catalina.loader.ParallelWebappClassLoader.getResource, via Tomcat documentation https://tomcat.apache.org/ Tomcat-8.0-doc/class-loader-howto.html see therefore, from the perspective of a Web application, class or resource loading Looks in the following repositories, in this OrdEr:bootstrap classes of your jvm/web-inf/classes of your web application/web-inf/lib/*.jar of your web applic ation System class Loader classes (described above) Common class loader classes (described above) the resource loading order is 1. Web-inf\classes 2.classpath 3. $CATALINA _base/lib
In the final analysis, Spring's resouceloader is based on our set of ClassLoader to load the resources