A: 026 – the JVM – the JVM delay loading https://yuhongliang.blog.csdn.net/article/details/111600183 for class
- The JVM’s underlying implementation of parental delegation is implemented for safe loading of classes, but is there a case in development where this parental delegation mechanism is broken? The answer is: yes
1. Break the principle of parental delegation
How does it work?
The loadClass() method implements the parent delegate logic of the ClassLoader. If you want to break the parent delegate logic, you have to use our own loadClass() method to write the ClassLoader. That makes it easy. Just override the loadClass() method of the ClassLoader!
Source code, this piece of writing is very light and clear.
Our goal is to invalidate this code and replace it with our own.
2. Currently, there are three cases of breaking parental delegation
Before Jdk1.2, custom classloaders need to override the loadClass() method
2.2 ThreadContextClassLoader base class can implement the call implementation class code, through the thread. SetContextClassLoader specified
2.3 Hot start, hot deployment
The idea or other tomcat tools are used to override the classloader. loaderClass method. Here’s an example:
2.3.1 Normal class loading
package com.yuhl.c2020.rebushu;
import com.yuhl.c2020.MyClassLoader;
/ * * *@author yuhl
* @Date 2020/12/26 17:08
* @Classname NomalClassLoader
* @DescriptionNormal deployment */
public class NomalClassLoader {
public static void main(String[] args) throws Exception {
MyClassLoader msbClassLoader = new MyClassLoader();
Class clazz = msbClassLoader.loadClass("com.yuhl.c2020.LoadClassResource");
msbClassLoader = null;
System.out.println(clazz.hashCode()); / / 1163157884
msbClassLoader = null;
msbClassLoader = new MyClassLoader();
Class clazz1 = msbClassLoader.loadClass("com.yuhl.c2020.LoadClassResource");
System.out.println(clazz1.hashCode()); / / 1163157884
System.out.println(clazz == clazz1); //true}}Copy the code
2.3.2 Hot Deployment Class Loading
package com.yuhl.c2020.rebushu;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/ * * *@author yuhl
* @Date 2020/12/26 17:08
* @Classname HotClassLoader
* @DescriptionHot deployment * /
public class HotClassLoader {
private static class MyClassLoaderHot extends ClassLoader {
@Override
publicClass<? > loadClass(String name)throws ClassNotFoundException {
File f = new File("D:/tmp/jvm/" + name.replace("."."/").concat(".class"));
if(! f.exists())return super.loadClass(name);
try {
InputStream is = new FileInputStream(f);
byte[] b = new byte[is.available()];
is.read(b);
return defineClass(name, b, 0, b.length);
} catch (IOException e) {
e.printStackTrace();
}
return super.loadClass(name); }}public static void main(String[] args) throws Exception {
MyClassLoaderHot m = new MyClassLoaderHot();
Class clazz = m.loadClass("com.yuhl.c2020.Miao");
m = new MyClassLoaderHot();
Class clazzNew = m.loadClass("com.yuhl.c2020.Miao");
System.out.println(clazz == clazzNew); //false}}Copy the code
By comparing 2.3.1 and 2.3.2 above, it’s easy to conclude that hot deployment really is possible.
3. Summary
The discussion here is more superficial, in order to understand the application scenarios and rationale for breaking the parental delegation mechanism. For an in-depth understanding, see In Depth Java Virtual Machine or other resources!
Next article: 028 – the JVM – class loading subsystem interview questions in actual combat, https://yuhongliang.blog.csdn.net/article/details/111799834