本文源代码在 Github 。
本文仅为个人笔记,不应作为权威参考。
原文
javadoc ClassLoader :
A class loader is an object that is responsible for loading classes.
...
Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class.
A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
简单来说:
讲到bootstrap class loader就不得不说三种常见的ClassLoader实现。
执行下面代码会看到三种类型的ClassLoader实现:
import com.sun.javafx.util.Logging; import java.util.ArrayList; public class PrintClassLoader { public static void main(String[] args) { System.out.println("Classloader for ArrayList: " + ArrayList.class.getClassLoader()); System.out.println("Classloader for Logging: " + Logging.class.getClassLoader()); System.out.println("Classloader for this class: " + PrintClassLoader.class.getClassLoader()); } }
结果如下:
Classloader for ArrayList: null Classloader for Logging: sun.misc.Launcher$ExtClassLoader@5e2de80c Classloader for this class: sun.misc.Launcher$AppClassLoader@18b4aac2
$JAVA_HOME/jre/lib
下的核心库和 rt.jar
。 $JAVA_HOME/lib/ext
目录和System Property java.ext.dirs
所指定目录下的类(见 Java Extension Mechanism Architecture
)。 sun.misc.Launcher
的构造函数里看到),负责加载 CLASSPATH
环境变量、 -classpath/-cp
启动参数指定路径下的类。 每个Class对象引用了当初加载自己的ClassLoader( javadoc ClassLoader ):
Every Class object contains a reference to the ClassLoader that defined it.
其实Class对象的 getClassLoader()
方法就能够得到这个ClassLoader,并且说了如果该方法返回空,则说明此Class对象是被bootstrap class loader加载的,见 getClassLoader() javadoc
:
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.
简单来说说了三点:
getClassLoader()
下面是一段实验代码:
import com.sun.javafx.util.Logging; public class PrintArrayClassLoader { public static void main(String[] args) { System.out.println("ClassLoader for int[]: " + new int[0].getClass().getClassLoader()); System.out.println("ClassLoader for string[]: " + new String[0].getClass().getClassLoader()); System.out.println("ClassLoader for Logging[]: " + new Logging[0].getClass().getClassLoader()); System.out.println("ClassLoader for this class[]: " + new PrintArrayClassLoader[0].getClass().getClassLoader()); } }
得到的结果如下,符合上面的说法:
ClassLoader for int[]: null ClassLoader for string[]: null ClassLoader for Logging[]: sun.misc.Launcher$ExtClassLoader@5e2de80c ClassLoader for this class[]: sun.misc.Launcher$AppClassLoader@18b4aac2
那如果是二维数组会怎样呢?下面是实验代码:
import com.sun.javafx.util.Logging; public class PrintArrayArrayClassLoader { public static void main(String[] args) { System.out.println("ClassLoader for int[][]: " + new int[0][].getClass().getClassLoader()); System.out.println("ClassLoader for string[][]: " + new String[0][].getClass().getClassLoader()); System.out.println("ClassLoader for Logging[][]: " + new Logging[0][].getClass().getClassLoader()); System.out.println("ClassLoader for this class[][]: " + new PrintArrayClassLoader[0][].getClass().getClassLoader()); System.out.println("ClassLoader for this Object[][] of this class[]: " + new Object[][]{new PrintArrayArrayClassLoader[0]}.getClass().getClassLoader()); } }
结果是:
ClassLoader for int[][]: null ClassLoader for string[][]: null ClassLoader for Logging[][]: sun.misc.Launcher$ExtClassLoader@5e2de80c ClassLoader for this class[][]: sun.misc.Launcher$AppClassLoader@18b4aac2 ClassLoader for this Object[][] of this class[]: null
注意第四行的结果,我们构建了一个 Object[][]
,里面放的是 PrintArrayArrayClassLoader[]
,但结果依然是null。所以:
ClassLoader本身也是类,那么是谁加载它们的呢?实际上ClassLoader类的ClassLoader就是bootstrap class loader。下面是实验代码:
import com.sun.javafx.util.Logging; public class PrintClassLoaderClassLoader { public static void main(String[] args) { // Launcher$ExtClassLoader System.out.println("ClassLoader for Logging's ClassLoader: " + Logging.class.getClassLoader().getClass().getClassLoader()); // Launcher$AppClassLoader System.out.println("ClassLoader for this class's ClassLoader: " + PrintClassLoaderClassLoader.class.getClassLoader().getClass().getClassLoader()); // 自定义ClassLoader System.out.println("ClassLoader for custom ClassLoader: " + DummyClassLoader.class.getClassLoader().getClass().getClassLoader()); } public static class DummyClassLoader extends ClassLoader { } }
结果是:
ClassLoader for Logging's ClassLoader: null ClassLoader for this class's ClassLoader: null ClassLoader for custom ClassLoader: null
简单来说ClassLoader就是解决类加载问题的,当然这是一句废话。JDK里的ClassLoader是一个抽象类,这样做的目的是能够让应用开发者定制自己的ClassLoader实现(比如添加解密/加密)、动态插入字节码等,我认为这才是ClassLoader存在的最大意义。
还是看 javadoc的说法 :
The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.
简单来说:
ClassLoader的委托模型存在这么一个问题:子ClassLoader能够看见父ClassLoader所加载的类,而父ClassLoader看不到子ClassLoader所加载的类。
这个问题出现在Java提供的SPI上,简单举例说明:
这是因为B一般都是在Classpath中的,它是被System class loader加载的,而SPI A是在核心库里的,它是被bootstrap class loader加载的,而bootstrap class loader是顶级ClassLoader,它不能向下委托给System class loader,所以SPI A是找不到实现B的。
这个时候可以通过 java.lang.Thread#getContextClassLoader()
和 java.lang.Thread#setContextClassLoader
来让SPI A加载到B。
为何SPI A不直接使用System class loader来加载呢?我想这是因为如果写死了System class loader那就缺少灵活性的关系吧。
如果一个类被一个ClassLoader加载两次,那么两次的结果应该是一致的,并且这个加载过程是线程安全的,见ClassLoader.java源码:
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) { // ... 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. // ... c = findClass(name); // ... } } // ... return c; } }
如果一个类被两个不同的ClassLoader加载会怎样呢?看下面代码:
// 把这个项目打包然后放到/tmp目录下 public class ClassUniqueness { public static void main(String[] args) throws Exception { Class<?> fooClass1 = Class.forName("me.chanjar.javarelearn.classloader.ClassUniqueness"); System.out.println("1st ClassUniqueness's ClassLoader: " + fooClass1.getClassLoader()); // 故意将parent class loader设置为null,否则就是SystemClassLoader(即ApplicationClassLoader) URLClassLoader ucl = new URLClassLoader(new URL[] { new URL("file:///tmp/classloader.jar") }, null); Class<?> fooClass2 = ucl.loadClass("me.chanjar.javarelearn.classloader.ClassUniqueness"); System.out.println("2nd ClassUniqueness's ClassLoader: " + fooClass2.getClassLoader()); System.out.println("Two ClassUniqueness class equals? " + fooClass1.equals(fooClass2)); } }
运行结果是:
1st ClassUniqueness's ClassLoader: sun.misc.Launcher$AppClassLoader@18b4aac2 2nd ClassUniqueness's ClassLoader: java.net.URLClassLoader@66d3c617 Two ClassUniqueness class equals? false```
观察到两点:
由此可以得出结论:一个Class的唯一性不仅仅是其全限定名(Fully-qualified-name),而是由【加载其的ClassLoader + 其全限定名】联合保证唯一。
这种机制对于解决诸如类冲突问题非常有用,类冲突问题就是在运行时存在同一个类的两个不同版本,同时代码里又都需要使用这两个不同版本的类。解决这个问题的思路就是使用不同的ClassLoader加载这两个版本的类。事实上OSGi或者Web容器就是这样做的(它们不是严格遵照委托模型,而是先自己找,找不到了再委托给parent ClassLoader)。