Understand ClassLoader in five minutes

java.lang.ClassLoaderClass overview:

image

From the introduction of the ClassLoader class in the document, it can be concluded that the role of this class is to find the corresponding Class bytecode file according to the fully qualified name of a specified class , and then load it into an instance of the java.lang.Class class .

The division of class loaders :

Bootstrap ClassLoader:

 This class loader is responsible for loading the class library in the <JAVA_HOME>\lib directory into the virtual machine memory to load the core library of java. This type of loader does not inherit from java.lang.ClassLoader and cannot be directly used by the java program. Call, the code is written in C++. It is a part of the virtual machine itself.

Extendsion ClassLoader:
 This class loader is responsible for loading the class library in the <JAVA_HOME>\lib\ext directory, which is used to load the extension library of java. Developers can directly use this class loader.

Application ClassLoader:

This class loader is responsible for loading the class library under the user class path (CLASSPATH). Generally, the java classes we write are loaded by this class loader. This class loader is the return value of the getSystemClassLoader() method in CLassLoader, so it is also It is called the system class loader. In general, this is the system default class loader.

In addition, we can also add our own class loader to meet special needs and need to inherit the java.lang.ClassLoader class.

  The hierarchical relationship between class loaders is as follows:


Observe the class loader with code:

package com.wang.test;

public class TestClassLoader {

    public static void main(String[] args) {
        ClassLoader loader = TestClassLoader.class.getClassLoader();
        System.out.println(loader.toString());
        System.out.println(loader.getParent().toString());
        System.out.println(loader.getParent().getParent());
    }
}

Observe the printing result:

sun.misc.Launcher$AppClassLoader@500c05c2
sun.misc.Launcher$ExtClassLoader@454e2c9c
null

The first line prints the application class loader (default loader), and the second line prints its parent class loader and extended class loader. According to our idea, the third line should print the startup class loader, here However, the returned null is due to getParent(). If it returns null, the startup class loader is used as the parent loader by default.

 The parent delegation model of the class loader:

The parent delegation model is a specification for organizing the relationship between class loaders. Its working principle is: if a class loader receives a request for class loading, it will not try to load this class by itself, but will The request is delegated to the parent class loader to complete, so layer by layer, and finally all loading requests are passed to the top startup class loader, only when the parent class loader cannot complete the loading request (its search range If the required class is not found), it will be handed over to the subclass loader to try to load.

The advantage of this is that the java class has a priority hierarchical relationship along with its class loader. This is very necessary, such as java.langObject, which is stored in \jre\lib\rt.jar, it It is the parent class of all java classes, so no matter which class is loaded, this class must be loaded. In the end, all loading requests are summarized in the top-level startup class loader. Therefore, the Object class will be loaded by the startup class loader, so the loaded They are all the same class. If the parent delegation model is not used and each class loader loads it by itself, there will be more than one Object class in the system, and the application will be completely messed up.

Class.forname()与ClassLoader.loadClass():

Class.forname(): is a static method, the most commonly used is Class.forname(String className); returns a Class object according to the fully qualified name of the passed-in class. This method loads the Class file into memory at the same time, it will Perform the initialization of the class.

如: Class.forName(“com.wang.HelloWorld”);

ClassLoader.loadClass(): This is an instance method that requires a ClassLoader object to call this method. When this method loads the Class file into memory, the initialization of the class will not be performed, and the initialization will not be performed until the class is used for the first time. Because this method needs to get a ClassLoader object, it can specify which class loader to use as needed.

如:ClassLoader cl=…….;cl.loadClass(“com.wang.HelloWorld”);


Guess you like

Origin blog.51cto.com/15082402/2644359
Recommended