Java class loader

Class loader description

The class loader is responsible for The class file is loaded into memory, and a Java. Java. XML file is generated for the class Lang. class instance.

Once a class is loaded into the JVM, the same class will not be added again. In the JVM, the unique identifiers used to judge the class are: class name, package name of the class and class loader.

When the JVM starts, an initial class loader hierarchy consisting of three class loaders is formed:

The root class loader is responsible for loading the core classes of Java (i.e. the jar package under java_home / JRE / LIB). When you use the - xbootclasspath option or use the - D option to specify the sun.boot.class.path system attribute, you can also specify to load additional classes. The root class loader is special. It is not a subclass of java.lang.classloader, but implemented by the JVM itself.

The extension class loader is responsible for loading the jar package under the extension directory of JRE (i.e. java_home / JRE / lib / ext directory).

The system class loader is responsible for loading the - classpath option or Java. Java from the Java command when the JVM is started class. Path system attribute, or the jar package and classpath specified by the classpath environment variable, or the current classpath. The program can obtain the system class loader through the static method getsystemclassloader () method of classloader.

In addition to the three types of loaders provided by Java, developers can also implement their own class loaders. Custom class loaders are implemented by inheriting classloader.

The hierarchy of these four kinds of loaders in the JVM is shown in the figure below:

As shown in the figure, the root class loader is the parent class loader of the extension class loader; The extension class loader is the parent class loader of the system class loader; If not specified, the user-defined class loader takes the system class loader as the parent loader.

It should be mentioned that the parent-child relationship between class loaders is not the parent-child relationship on class inheritance, but the relationship between class loader instances.

Class loading mechanism

JVM class loading mechanisms mainly include the following three mechanisms:

There are 8 steps for the class loader to load classes:

Custom class loader

All class loaders in the JVM except the root class loader are instances of subclasses of classloader. Developers can implement custom class loaders by inheriting classloader and overriding classloader methods.

The classloader class has three key methods:

To implement a custom classloader, you can override loadclass or findclass. However, it is generally recommended to override findclass instead of loadclass. Take a look at the execution steps of loadclass:

From the above, overriding findclass can avoid overriding the parent class delegation and caching mechanism of the default class loader.

The following is an example of a custom class loader:

Using a custom class loader, you can achieve the following functions:

Urlclassloader class

Urlclassloader is the parent of the extension class loader class and the system class loader class. Urlclassloader is quite powerful. You can get java files locally to load classes, or get remote files to load classes.

Demonstrate:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>