JVM 通过 class loader 和 class 命名唯一确定类。
双亲委派机制中,父子关系通过组合完成,而不是继承!!!
好处:
- 共享功能:可以避免重复加载,当父亲已经加载了该类的时候,子类不需要再次加载,一些 Framework 层级的类一旦被顶层的 ClassLoader 加载过就缓存在内存里面,以后任何地方用到都不需要重新加载。
- 隔离功能:主要是为了安全性,避免用户自己编写的类动态替换 Java 的一些核心类,比如 String ,同时也避免了重复加载,因为 JVM 中区分不同类,不仅仅是根据类名,相同的 class 文件被不同的 ClassLoader 加载就是不同的两个类,如果相互转型的话会抛
java.lang.ClassCaseException
。
Java 9 after
Java9 模块化之后,移除了很多老旧包,重命名了 System ClassLoader 和 ExtensionClassLoader。
新的关系是 system class loader → builtin class loader → platform → bootstrap classloader,builtin 主要做一些模块衔接,双亲委派模型基本没变。
https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-A868D0B9-026F-4D46-B979-901834343F9E
https://www.oracle.com/technetwork/java/javase/9-relnotes-3622618.html
- The
application class loader
is no longer an instance of URLClassLoader
but, rather, of an internal class. It is the default loader for classes in modules that are neither Java SE nor JDK modules.
- The extension class loader has been renamed; it is now the platform class loader. All classes in the Java SE Platform are guaranteed to be visible through the platform class loader. In addition, the classes in modules that are standardized under the Java Community Process but not part of the Java SE Platform are guaranteed to be visible through the platform class loader.
Just because a class is visible through the platform class loader does not mean the class is actually defined by the platform class loader. Some classes in the Java SE Platform are defined by the platform class loader while others are defined by the bootstrap class loader. Applications should not depend on which class loader defines which platform class.
The changes in JDK 9 may impact code that creates class loaders with null (that is, the bootstrap class loader) as the parent class loader and assumes that all platform classes are visible to the parent. Such code may need to be changed to use the platform class loader as the parent (see ClassLoader.getPlatformClassLoader).
The platform class loader is not an instance of URLClassLoader, but, rather, of an internal class.
- The bootstrap class loader is still built-in to the Java Virtual Machine and represented by null in the ClassLoader API. It defines the classes in a handful of critical modules, such as java.base. As a result, it defines far fewer classes than in JDK 8, so applications that are deployed with -Xbootclasspath/a or that create class loaders with null as the parent may need to change as described previously.
Java9 之后, bootstrap 负责范围更小了,以前使用 null 加载一些类要改为 platform class loader。
Extention class loder 改为从 classpath 加载类, java.ext.dirs 和 ext/ 被废弃。
继承关系图
