Parent delegation model: boot loader, extension loader, application loader

When it comes to the parent delegation model, first of all, we need to know what a class loader is.

The class loader loads the class file into the JVM memory according to the specified fully qualified name and converts it into a Class object.

The class loader in Java is mainly used to implement the loading of classes. Classes in Java and classloaders together uniquely determine the uniqueness of a class within the JVM.

Class loaders can be divided into two categories, boot class loaders and other class loaders, among which Bootstrap Class Loader: implemented in C++, which is part of the JVM, and other class loaders: implemented by the Java language, Inherited from the abstract class ClassLoader, other class loaders are divided into extension class loaders and application loaders.

Bootstrap ClassLoader: It is implemented in C++ and is part of the JVM. Other loaders are implemented in Java and are independent of the JVM. Mainly responsible for loading the class library in the lib directory under <java home> or the class library in the path specified by the -Xbootclasspath parameter. The application cannot directly use this loader

Extended class loader: Responsible for loading classes under lib/ext under Java home or class libraries under the path specified by the class system variable java.ext.dirs. Developers can use this loader directly.

Application class loader: It is mainly responsible for loading the class library under the path specified by the user, namely the classPath. If the application does not have a custom class loader, this loader is used by default. (loadClass, findClass, defineClass)

The parent delegation model requires that in addition to the top-level startup class loader, other class loaders have their own parent class loader, use the composition relationship to reuse the parent class loader, and delegate requests to the parent class loader. It's all like that. So all loads end up in the top-level startup class loader. Only when the parent class loader reports that it cannot be loaded (ClassNotFoundException), will the loading task be given to the child loader.

benefit:

1. Make the Java class have priority along with its class loader. Taking the object class as an example, any request to load an object will reach the startup class loader, so that the object is the same class in various class loaders. If it is not for this model, and the user defines the object class, the system will appear multiple object classes, resulting in confusion.

2. The parent delegation model can ensure the stability of Java program operation, but the implementation is simple.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325150915&siteId=291194637