In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "what is class loader and parent delegation mechanism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
(1) Overview
We all know that the Java code will be compiled into a class file, which describes all kinds of information about this class in the class file, and the class class will eventually need to be loaded into the virtual machine before it can be run and used.
The virtual machine loads the Class file into memory, verifies, transforms, parses and initializes the data, and finally forms the Java type that the virtual machine can use directly, which is the class loading mechanism of the virtual machine.
(2) the process of class loading
A class consists of the following seven phases from being loaded to unloaded out of memory:
The sources for loading, verifying, preparing, parsing, initializing, using, and unloading loads are as follows:
1. Local disk
2. .class files downloaded from the network
3. Load .class files under war,jar
4. Read .class files from a special database (rare)
5. Dynamically compile java source files into class files, which are typically dynamic agents, and generate class files at run time.
The loading process is implemented through the class loader. I'll cover other procedures for class loading in the next chapter.
(3) Classification of class loaders
Class loaders are divided into system level and user level:
System-level class loaders are:
1. Start the classloader (the underlying layer is implemented by C++)
2. Extension class loader (the underlying implementation is java, which is a subclass of ClassLoader)
3. Application class loader (the underlying implementation is java, which is a subclass of ClassLoader)
The user-level classloader is called the custom classloader.
3.1 start the classloader
First, let's take a look at which classes are loaded by the startup class loader, which is responsible for loading sun.boot.class.path:
Public static void bootClassLoaderLoadingPath () {/ / get the directory String bootStrapLoadingPath=System.getProperty ("sun.boot.class.path") loaded by the boot column loader; / / convert the loaded directory to the collection List bootLoadingPathList= Arrays.asList (bootStrapLoadingPath.split (";"); for (String bootPath:bootLoadingPathList) {System.out.println ("startup class loader loaded directory:" + bootPath);}}
From the above code, we can get the classes loaded by the startup class loader:
3.2 extended class loader
The extension class loader load is responsible for loading the java.ext.dirs, and we also write a piece of code to load it:
Public static void extClassLoaderLoadingPath () {/ / get the directory String bootStrapLoadingPath=System.getProperty ("java.ext.dirs") loaded by the boot column loader; / / convert the loaded directory to the collection List bootLoadingPathList= Arrays.asList (bootStrapLoadingPath.split (";"); for (String bootPath:bootLoadingPathList) {System.out.println ("directory loaded by the extension class loader:" + bootPath);}}
You can see that in addition to loading the ext under the JDK directory, the ext under the Sun directory is also loaded
3.3 Application Class Loader
Finally, there is the application class loader, which loads the java.class.path:
Public static void appClassLoaderLoadingPath () {/ / get the directory String bootStrapLoadingPath=System.getProperty ("java.class.path") loaded by the boot column loader; / / convert the loaded directory to the collection List bootLoadingPathList= Arrays.asList (bootStrapLoadingPath.split (";"); for (String bootPath:bootLoadingPathList) {System.out.println ("directory loaded by the application class loader:" + bootPath);}}
It is responsible for loading the class and jar packages under classpath under the project directory.
(4) Parental assignment model
The so-called parent delegation model means that after a class receives a class load request, it passes the request to the parent class loader in turn (if any). If the top-level parent class loader can load, it returns successfully, if it cannot be loaded, and then loads it to the child loader in turn. Let's first look at the hierarchy of the class loader in terms of code:
Public class ClassLoaderPath {public static void main (String [] args) {System.out.println (ClassLoaderPath.class.getClassLoader ()); System.out.println (ClassLoaderPath.class.getClassLoader (). GetParent ()); System.out.println (ClassLoaderPath.class.getClassLoader (). GetParent (). GetParent ());}}
Write a class and output the class loader, the parent class loader, and the parent class loader of the class
You can see that the first is the application class loader, whose parent class is the extension class loader. The parent class of the extension class loader outputs a null, which null calls the startup class loader. If you don't believe me, let's take a look at the source code: ClassLoader class
Then findClass is called down from the parent class loader, and if it can be loaded, it is returned directly to class, and if it cannot be loaded, it goes down in turn. If the custom loader still fails to load, a ClassNotFound exception is thrown.
I drew a flowchart to show the whole process of the parent delegation model:
The parental delegation model ensures the stable operation of Java programs, avoids repeated loading of classes, and ensures that the core API of Java will not be tampered with.
(5) sabotaging the appointment of parents
The parental delegation model is not absolute, and the spi mechanism can break the parental delegation model.
First of all, we need to understand what spi,spi (Service Provider Interface) is a service discovery mechanism. Java defines many interfaces in the core library and gives the call logic for these interfaces, but does not give a specific implementation. All the developer has to do is customize an implementation class and register the implementation class information in META-INF/services for use by the core class library. The most typical one is JDBC.
Java provides a Driver interface to drive database connections from various vendors. The driver class is located in the jre/lib/rt.jar in JAVA_HOME and should be loaded by the Bootstrap class loader. According to the class loading mechanism, when the loaded class references another class, the virtual machine will use the class loader that loads the class to load the referenced class, so if other database vendors customize the Driver implementation class, it is obviously unreasonable to put the implementation class in the directory where the class loader starts.
So Java provides the spi mechanism, even if the Driver is loaded by the startup class loader, but he can ask the thread context loader (Thread Context ClassLoader) to request the subclass loader to complete the loading, which is the application class loader by default. But it does break the class loading mechanism.
This is the end of the content of "what is classloader and parent delegation mechanism". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.