Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java class loading mechanism and parent delegation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what is the Java class loading mechanism and parent delegation". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the Java class loading mechanism and parent delegation?"

Class loader history

In Java 1.1 and earlier, there was no relationship between class loads. For example, the system class loader is responsible for loading the application and the class files and resources in the classpath directory, while the applet class loader is responsible for interacting with the server to load the applets application and its related class files and resources.

In J2SE 1.2 (if you know the name J2SE, it proves that you are an old programmer, ), there is a relationship between classloaders, which is known as parent delegation (parent delegation) mechanism.

What is the assignment of parents?

To put it simply, the parent assignment mechanism is that when the current class loader goes to load an class data, it will first delegate its parent loader to do this, and the parent loader will recursively delegate its own parent loader to load until the parent loader does not exist, or the parent loader cannot load it (Note: the parent loader here is not an inheritance relationship in Java, but a responsibility relationship).

The following three common classloaders are available in JDK:

BootstrapClassLoader: commonly known as boot classloader, it is the top-level classloader, also known as root classloader. It loads the class file in JRE/lib/rt.jar, and the loading directory can be changed by-Xbootclasspath.

ExtClassLoader: commonly known as the extension class loader, it is responsible for loading class files in the JRE/lib/ext directory. You can change the loading directory by setting the environment variable java.ext.dirs, giving priority to BootstrapClassLoader.

AppClassLoader: commonly known as application class loader, also known as system class loader, is responsible for loading our application class files and class files in the directory specified by classpath environment variables, giving priority to ExtClassLoader.

The advantage of this mechanism is that it can clearly divide the responsibilities of each kind of adders and ensure the uniqueness of class loading. when a class file is loaded by its parent loader, the subsequent class loader will not load.

The disadvantages of the parental appointment mechanism

It also has some shortcomings, such as Java's SPI mechanism, which is not well supported by the parent delegation mechanism, so the context class loader is introduced.

SPI, whose full name is Service Provider Interface, is a specification of Java Discovery Service. JDK is responsible for providing the interface specification of the service, and third-party vendors are responsible for implementing the service. For example, the well-known JDBC is implemented by this mechanism.

The interface specification of JDBC is defined by JDK in rt.jar. We know that class in this jar is loaded by BootstrapClassLoader, while the implementation class of JDBC is loaded by AppClassLoader. So when the JDBC interface needs to use the implementation class, it will not be able to complete the operation, but the chicken thief Java gods introduced thread context class loader to solve this problem.

If you do not make special settings, usually the context class loader of a thread is the system class loader, that is, AppClassLoader, which happens to load the class file of the implementation class provided by the manufacturer. Interested students can refer to some of the source code in DriverManager under the java.sql package in JDK as follows:

/ / Worker method called by the public getConnection () methods.

Private static Connection getConnection

(

Throws SQLException {

/ *

* When callerCl is null, we should check the application's

* (which is invoking this class indirectly)

* classloader, so that the JDBC driver class outside rt.jar

* can be loaded from here.

ClassLoader callerCL = caller! = null? Caller.getClassLoader (): null

Synchronized (DriverManager.class) {

/ / synchronize loading of the correct classloader.

If (callerCL = = null) {

CallerCL = Thread.currentThread () .getContextClassLoader ()

}

/ * * omit part of the source code * /

}

From the above, we learned about the division of several types of loaders in JDK, and also discussed the nature of the parent delegate loading mechanism. Next, let's take a look at the steps a class file goes through before it is loaded into the Java runtime environment and becomes a usable java.lang.Class instance.

Class loading step

When a class file becomes a usable Class instance in the Java runtime environment, it mainly goes through three steps: loading, linking, and initialization.

1. Load

A total of three things will be done at this stage:

1. The binary byte stream that defines the class is obtained through the fully qualified name of the class.

two。 Converts a byte stream to an JVM runtime data structure.

3. Generate a Class instance representing the class in JVM for subsequent use.

two。 Link

This stage is mainly divided into three steps: verification, preparation and parsing.

Verification is the first step of the link. The first step is to verify the file format to confirm whether the class file is consistent with the current virtual machine specification, such as starting with the magic number 0xCAFEBABE, the class version number is within the scope of the current virtual machine processing, and so on. The second is to analyze the code semantics and confirm that the semantics of its description and the Java language specification

Preparation is the second step in the link, which allocates memory for the class variable (static modifier) and, if it is a constant (static final modifier), initializes it directly to the target constant.

Parsing is the third step in linking, in which the virtual machine replaces symbolic references in the constant pool with direct references.

3. Initialization

This stage is closest to programmer coding, mainly performing initialization of all class variables and static code blocks, while the virtual machine ensures that the parent class (except the interface) is initialized before the subclass initialization operation. The interface is initialized only when the static attribute of the interface is used directly.

At this point, I believe you have a deeper understanding of "Java class loading mechanism and parent delegation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report