< Span lang= "ZH-CN" >classloader classloader classloader The classes managed in classloader path to differentiate different classes. So also because there is a specific classloader JVM
a start the ClassLoader ( bootstrapclassloader
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; responsible for storing the <java_home>\lib -xbootclasspath parameter and is recognized by the virtual machine (only by file name, such as rt.jar lib java
The bootstrapclassloader is used to load a class when the JVM is started so that the JVM works properly, so it is implemented with native code, and is created at the earliest.
two . Extension class loader ( Extensionclassloader )
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; The developer is responsible for loading all class libraries in the <java_home>\lib\ext directory, or in the path specified by the JAVA.EXT.DIRS system variable, which developers can use directly.
Extension ClassLoader Search for a specific standard extension directory. The standard extension directory is not necessarily the same in different JVM implementations, and in Sun 's JVM It is %jre_home% \ Lib\ext, the specific path can be obtained by java.ext.dirs The system attribute value. The standard extended directory exists to be extended and shared, and application vendors can place portions of the shared library here instead of multiple copies in the directory of their respective programs. During the development process, we can also place some of the most common libraries here, without having to configure the environment every time.
three . Application class Loader ( Applicationclassloader )
is responsible for loading the class library specified on the user path, the developer can directly use the ClassLoader, and is the default class loader. Systemclassloader Search for directories and jar files configured in Classpath.
four . three kinds of loaders relationship with parent delegation
the JVM defaults to the parental delegation mechanism when loading classes. In layman's words, when a particular class loader receives a request to load a class, it first delegates the load task to the parent class loader, recursively, and returns if the parent ClassLoader can complete the class load task, but only when the parent class loader is unable to complete the load task, except for the top-level startup accumulator, The other class loaders have their own parent ClassLoader. It is important to note that the parent-child relationships of these classloader are not typically implemented in an inherited relationship, but rather are the code that is used by a combined relationship to go back to the parents loader.
The order of the ClassLoader is:
First bootstrap ClassLoader, then extension ClassLoader, and finally S. Ystem ClassLoader. from the point of view of design, this is actually a kind of responsibility chain pattern (Chain ofresponsibility pattern).
Why does Java need this model?
But if this is the case, it is not enough to secure the load of the class. Because users can completely define their own classloader, in their own classloader to destroy the proxy model, then the user's own written java.lang.Object can be loaded. To solve this problem, consider two instances of the same class name in Java, if they are loaded differently, then they are different types, that is, they cannot be converted, and the instanceof operator returns false. ClassLoader In this way, even if the user loads his own Java.lang.Object class, it is not a java.lang.Object that the system believes can be relied upon, then it is bypassing the security check mechanism.
v. Other
java designers recommend to developers the way the ClassLoader is implemented. In the java jndi jce jaxb jbi
Now the big hot OSGI also violates the principle of parental delegation, using a more complex website structure. Since OSGI is the key to modular thermal deployment, a custom ClassLoader, each module has its own classloader, and when the bundle is replaced , the bundle Replace the same loader with a hot replacement for the code.
These things about the principle, we also want to understand a little bit, "not afraid of not knowing that do not know." We understand the process by which we can better understand the parental delegation mechanism and internal system execution of Java.
Deficiencies, please correct me.
Java Foundation--classloader