Class life cycle
“Seder prepared western dishes”, i.e., home (loading) feast (verification) prepared (preparation) western (analytic) style (initialization) dishes
- Loading, realize network Loading, hot Loading, etc., need to customize the ClassLoader overwrite method in this stage
- Get the binary byte stream that defines this class by its fully qualified name (this part is abstracted by the JVM team as a ClassLoader and provided to users for extension) [7]
- Transform the static storage structure represented by this byte stream into the runtime data structure of the method area
- Generate a java.lang.Class object in memory that represents the Class and acts as an access point for the Class’s various data in the method area
- Verification, which checks that the bytes are well-formed (such as the magic number 0xCAFEBABE) to ensure that the code does not perform illegal operations
- Preparing, allocating space for static variables and initializing zero values
- ResolutionSymbolic references (such as references to classes or object domains) are resolved to direct references at run time
- Performs Initialization of a class (static block code execution, static field assignment)
- Class initialization does not necessarily happen when a class is loaded, so it does not necessarily happen when a static code block is called,Reference code
- New Generates an instance of the class
- Read the static field of the class
- Call a static method of a class
- Reflection calls
- When subclasses are loaded
- Call main
- Class initialization does not occur when,Reference code
-
A reference to a static field of a parent class by a subclass only triggers initialization of the parent class, not the subclass
-
Defining an array of objects
-
Reference static final constants
-
Get the Class object by the Class name
-
When loading a Class via class.forname, specify Initialize =false
-
Through the default loadClass method of ClassLoader
-
- Class initialization does not necessarily happen when a class is loaded, so it does not necessarily happen when a static code block is called,Reference code
To support the Runtime binding features of the Java language, the parsing phase may not begin until after the initialization phase.
ClassLoader
introduce
The class loader is
- The virtual machine team implemented the class-loading action of getting the binary byte stream that defines this class by its fully qualified name outside of the JVM
- So that the application can decide how to get the classes it needs
The code module that implements this action is abstracted as a “ClassLoader”. That is to say,
- The JVM team uses the ClassLoader to outsource some of the loading phase functionality to the application developers themselves. [1]
Since classloaders are exposed to developers, there need to be some ground rules to prevent some of the JVM’s important class loading from interfering, so the parent-Delegation Model was introduced.
Parent-delegation Model
The structure of the parent delegate model is shown below.
public void printClassLoaders(a) throws ClassNotFoundException {
System.out.println("Classloader of this class:"
+ PrintClassLoader.class.getClassLoader());
System.out.println("Classloader of Logging:"
+ Logging.class.getClassLoader());
System.out.println("Classloader of ArrayList:"
+ ArrayList.class.getClassLoader());
}
Copy the code
Executing the above code results in the following output, which shows that different classes use different Classloaders.
Class loader of this class:sun.misc.Launcher$AppClassLoader@ 18b4aac2
Class loader of Logging:sun.misc.Launcher$ExtClassLoader@ 3caeaf62
Class loader of ArrayList:null
Copy the code
Class loaders are classified as follows [1][2] :
- Bootstrap ClassLoader
- The internal classes responsible for loading the JDK (rt.jar, etc.) are located
$JAVA_HOME/jre/lib
- Native code, which is responsible for loading other classloaders, is the parent of all classLoaders
- The internal classes responsible for loading the JDK (rt.jar, etc.) are located
- Extension ClassLoader
- Responsible for loading
$JAVA_HOME/lib/ext
orjava.ext.dirs
The position indicated by the system variable
- Responsible for loading
- Application ClassLoader
- Also known as the system ClassLoader, which loads classes in the -classpath or -cp directory and is a child loader of the Extension ClassLoader
The details of the parent delegate model can be found here, which basically means that the parent loader takes precedence over class loading, and that there is only one set of internal core classes, such as Object. Features:
- System classes prevent multiple copies of the same bytecode from appearing in memory
- Prevent core classes from being tampered to ensure the safe and stable running of Java programs
- Visibility, the child loader can see the classes loaded by the Parent, but the Parent loader can’t see below (hence JDBC’s need to break the parent-Delegation Model)
ClassLoader#loadClass(String name)
The various methods of ClassLoader
- LoadClass, which is called by the JVM, can be understood as the entry to the ClassLoader. The implementation of the parent delegate model is in this method, which loads classes based on fully qualified names. If the parent cannot handle it, the current ClassLoader executes the findClass method
- Overriding this method breaks the parental delegate pattern and supports hot loading
- FindClass, which loads the class by the fully qualified name, calls the defineClass method
- Overriding this method loads Class bytecode from the network
- DefineClass, which converts bytecode to Class, we don’t need to override, nor can we override this method (because it is already defined as final).
Customize the usage scenarios of ClassLoader
- Resource isolation
- If tomcat, the sea
- Dependency conflict handling
- Pandora: Each middleware uses a custom loader. Based on the configuration information, the middleware puts the classes it needs to expose into a Hashmap that the business code holds
- Hot deployment
- Tomcat and Spring Boot DevTools are recommended
- Code to protect
- You have to break the parent delegate — JDBC
reference
1. This article is enough for the JVM class loading mechanism
2, Class Loaders in Java | Baeldung
Java – How Tomcat Classloader carnival different Webapps object scope in same JVM? – Stack Overflow
4. I didn’t expect that even the ClassLoader could ask so many questions in the interview… . – Java bosom friend number – Blog garden
5, Java – Why the book says JDBC damage the Parental model? – Stack Overflow
6, Why do JDBC and Tomcat destroy the parental model? – Katastros
7. Class Loading-IBM Documentation
8, Chapter 5
9. What are the actual usage scenarios of Java ClassLoader? – zhihu
10. Analysis of class uninstallation, hot replacement, and Tomcat hot deployment