本文中JDK安装目录为:E:\Java\jdk1.7.0_60
1、Bootstrap classloader 根类加载器,使用系统本地语言编写,无法获取到;主要负责加载位于JDK安装目录 Java\jdk1.7.0_60\jre\lib下的类,比如rt.jar,其中包括
sun.misc.Launcher、ExtClassLoader、AppClassLoader
Launcher.class 官方网站找不到只能使用反编译工具JD-GUI,进行反编译;
public class ClassLoaderTest { public static void main(String[] args) { sun.misc.URLClassPath path = sun.misc.Launcher.getBootstrapClassPath(); for (URL url : path.getURLs()) { System.out.println(url.toString()); } } }
file:/E:/Java/jdk1.7.0_60/jre/lib/resources.jar file:/E:/Java/jdk1.7.0_60/jre/lib/rt.jar file:/E:/Java/jdk1.7.0_60/jre/lib/sunrsasign.jar file:/E:/Java/jdk1.7.0_60/jre/lib/jsse.jar file:/E:/Java/jdk1.7.0_60/jre/lib/jce.jar file:/E:/Java/jdk1.7.0_60/jre/lib/charsets.jar file:/E:/Java/jdk1.7.0_60/jre/lib/jfr.jar file:/E:/Java/jdk1.7.0_60/jre/classes
2、ExtClassLoader 扩展类加载器 ,负责加载 E:\Java\jdk1.7.0_60\jre\lib\ext
3、AppClassLoader 系统类加载器 ,负责加载项目中classpath 下的类,主要包括项目中引入的第三方类库和自己编写的类;
import org.apache.logging.log4j.Logger; public class ClassLoaderTest { public static void main(String[] args) { ClassLoader loader = ClassLoaderTest.class.getClassLoader(); System.out.println("自己编写的类的类加载器---"+loader.getClass().getName()); ClassLoader thirdClassLoader = Logger.class.getClassLoader(); System.out.println("第三方日志类库org.apache.logging.log4j.Logger的类加载器---"+thirdClassLoader.getClass().getName()); } }
运行结果:
自己编写的类的类加载器---sun.misc.Launcher$AppClassLoader 第三方日志类库org.apache.logging.log4j.Logger的类加载器---sun.misc.Launcher$AppClassLoader
1.AppClassLoader类中有一个引用指向ExtClassLoader ;ExtClassLoader类中也有一个引用指向BootstrapClassLoader,由于BootstrapClassLoader是系统本地语言编写,无法用Java获取,所有引用为null
public static void main(String[] args) { ClassLoader loader = ClassLoaderTest.class.getClassLoader(); System.out.println("AppClassLoder---"+loader.getClass().getName()); ClassLoader parentLoader = loader.getParent(); System.out.println("AppClassLoder parent---"+parentLoader.getClass().getName()); ClassLoader bootstrapLoader = parentLoader.getParent(); System.out.println("ExtClassLoader parent---"+bootstrapLoader); }
AppClassLoder---sun.misc.Launcher$AppClassLoader AppClassLoder parent---sun.misc.Launcher$ExtClassLoader ExtClassLoader parent---null
2、AppClassLoader和ExtClassLoader 都继承自URLClassLoader
URLClassLoader 继承自SecureClassLoader
SecureClassLoader继承自ClassLoader
3、类加载的过程:自底向上,其中AppClassLoader 和ExtClassLoader 的loadClass方法都是调用父类的
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded 检查是否加载过 Class c = findLoadedClass(name); if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) {//调用上一级去加载 c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { // If still not found, then invoke findClass in order // to find the class. 上一级没有加载成功,由自己进行加载 long t1 = System.nanoTime(); c = findClass(name); // this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { resolveClass(c); } return c; } }
a、首先 AppClassLoader 查找是否已经加载过,如果找到了,直接返回Class;
b、如果没有找到,找上一级类加载器ExtClassLoader查找,如果找到了,直接返回Class;
c、如果没有找到由BootStrapClassLoader 进行加载,加载成功直接返回Class;
d、如果没有加载成功,由下一级ExtClassLoader负责加载,加载成功直接返回Class;
e、如果没有加载成功,由下一级AppClassLoader负责加载,加载成功直接返回Class;
f、如果没有加载成功,抛出异常ClassNotFoundException;
以上就是Java中的双亲委托机制;