0.1, Spring source learning -JVM class loader - parent delegation model -Spring class loading mechanism

Copyright Notice: Welcome to reprint exchange, to declare the source. Performance status prior to the state of mind, habits prior to determination, focus first on preferences --Bestcxx https://blog.csdn.net/bestcxx/article/details/90513633

JVM class loading mechanism

/**
* 获取类加载器的测试方法
*/
@Test
public void test2() {
	//获取Test类的类加载器 sun.misc.Launcher$AppClassLoader@4dc63996
	ClassLoader c  = Test.class.getClassLoader();  
       System.out.println(c); 
       //获取c这个类加载器的父类加载器 sun.misc.Launcher$ExtClassLoader@28a418fc
       ClassLoader c1 = c.getParent(); 
       System.out.println(c1); 
       // getClassLoader() returning null indicates the bootstrap ClassLoader
       //获取c1这个类加载器的父类加载器 ,null,因为Bootstrp loader是C++编写的,依java的观点来看,逻辑上并不存在Bootstrap Loader的类实体
       //根装载器:Bootstrp loader
       //用C++语言写的,它是在Java虚拟机启动后初始化的,主要负责加载%JAVA_HOME%/jre/lib,
       //* -Xbootclasspath参数指定的路径以及%JAVA_HOME%/jre/classes中的类。
       ClassLoader c2 = c1.getParent();
       System.out.println(c2);
       
       //获取系统默认的ClassLoader sun.misc.Launcher$AppClassLoader@4dc63996
       ClassLoader c3=ClassLoader.getSystemClassLoader();
       System.out.println(c3);
       //获取创建当前线程的类加载器 
       ClassLoader c4= Thread.currentThread().getContextClassLoader();
       System.out.println(c4);
}

Parent delegation model

It simply is, first class needs its own parent class loader, has been passed up, if no longer by themselves loader
class loader can override, but the core classes are not allowed to use its own class loader loads, such as core jdk package java.lang.String, loaded only bootstrap ClassLoader

Spring class loading mechanism

入口 AbstractApplicationContext.prepareBeanFactory
beanFactory.setBeanClassLoader(getClassLoader());
DefaultResourceLoader.getClassLoader()

org.springframework.core.io.DefaultResourceLoader

/**
* Return the ClassLoader to load class path resources with.
 * <p>Will get passed to ClassPathResource's constructor for all
 * ClassPathResource objects created by this resource loader.
 * @see ClassPathResource
 */
@Override
public ClassLoader getClassLoader() {
	//如果指定了类加载器就返回指定的类加载器,否则获取线程类加载器->不存在就获取ClassUtils类加载器-> 不存在就获取系统默认类加载器
	return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}
ClassUtils.getDefaultClassLoader()

org.springframework.util.ClassUtils.getDefaultClassLoader ()
to see the Chinese comments

/**
* Return the default ClassLoader to use: typically the thread context
 * ClassLoader, if available; the ClassLoader that loaded the ClassUtils
 * class will be used as fallback.
 * <p>Call this method if you intend to use the thread context ClassLoader
 * in a scenario where you clearly prefer a non-null ClassLoader reference:
 * for example, for class path resource loading (but not necessarily for
 * {@code Class.forName}, which accepts a {@code null} ClassLoader
 * reference as well).
 * @return the default ClassLoader (only {@code null} if even the system
 * ClassLoader isn't accessible)
 * @see Thread#getContextClassLoader()
 * @see ClassLoader#getSystemClassLoader()
 */
public static ClassLoader getDefaultClassLoader() {
	ClassLoader cl = null;
	try {
		//获取当前线程类加载器
		cl = Thread.currentThread().getContextClassLoader();
	}
	catch (Throwable ex) {
		// Cannot access thread context ClassLoader - falling back...
	}
	//如果线程类加载器为null
	if (cl == null) {
		// No thread context class loader -> use class loader of this class.
		//获取当前类的类加载器
		cl = ClassUtils.class.getClassLoader();
		if (cl == null) {
			// getClassLoader() returning null indicates the bootstrap ClassLoader
			//getClassLoader() 为 null 说明是 bootstrap ClassLoader 类加载器,该加载器为 C++ 编写,故从Java 角度看是 null
			try {
				//获取当前系统的类加载器
				cl = ClassLoader.getSystemClassLoader();
			}
			catch (Throwable ex) {
				// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
			}
		}
	}
	return cl;
}

Guess you like

Origin blog.csdn.net/bestcxx/article/details/90513633