Depth understanding of java virtual machine (8): class loader

1, the virtual machine class loader stage to get the description of the class action binary stream into the java virtual machine except by qualified name of a class, to allow the application to determine how to obtain the required classes that implement this type of action It is called class loader. For any need to have a class together to establish its uniqueness in the java virtual machine in its class loader and the class itself, each class loader has a separate namespace class, two classes are equal under the premise that they made the same class loader loads.

package org.xiaofeiyang.classloader;

import java.io.IOException;
import java.io.InputStream;

/**
* @author: yangchun
* @description:
* @date: Created in 2019-11-23 22:39
*/
public class ClassLoaderTest {
public static void main(String[] args) throws ClassNotFoundException {
ClassLoader myLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
String fileName = name.substring(name.lastIndexOf(".")+1)+".class";
InputStream is = getClass().getResourceAsStream(fileName);
if(is==null){
return super.loadClass(name);
}
byte[] res = new byte[is.available()];
is.read(res);
return defineClass(name,res,0,res.length);
}catch (IOException e){
throw new ClassNotFoundException(name);
}
}
};
Object obj = myLoader.loadClass("org.xiaofeiyang.classloader.ClassLoaderTest");
System.out.println(obj.getClass());
System.out.println(obj instanceof org.xiaofeiyang.classloader.ClassLoaderTest);
}
}
输出如下

class java.lang.Class
false

2, parent delegation model

Two types of loader, boot class loader, one is to start class loader, the class loader is part of the virtual machine ++ implemented by c, and the other is for all other class loaders, implemented by the java language itself, independent of to the java virtual machine, all inheritance and abstract classes java.lang.classLoader.

1) Start class loader is responsible for the java_home the lib folder file java virtual machine can recognize the rt.jar library is loaded into the virtual machine's memory.

2) extension class loader, the class loader is implemented by sun.misc.Launcher $ ExtClassLoader, this general class loader is responsible for loading java_home \ lib \ ext lower class library, developers can directly use this class loader.

3) application class loader, class loader is implemented by sun.misc.Launcher $ App-ClassLoader, the class loader is the return value of the ClassLoader getSystemClassLoader () method, so called system class loader is responsible for loading ships library, under application classpath typically use the default class loader.

 

 This relationship between the class hierarchy is referred to as a parent delegation model, in addition to the top-level parent delegation model requires the rest of the class loader must have a parent class loader, using a combination of approaches to multiplex the parent class loader code. Subclass the parent class will receive a load request to load, if the load will not take the initiative themselves to be loaded. Benefit is the priority, rt in the class will not be replaced, to ensure the stable operation of the java virtual machine. Parent delegation model code in the code as follows java.lang.ClassLoader

 

 Parent delegation model good solution to the base class of each class loader reunification.

Parent delegation model is destroyed following three conditions

1) inherited java.lang.ClassLoader rewrite the loadClass method to avoid the problem needs to be rewritten findClass method

2) jndi code is loaded by the boot class loader, the code jndi interface provider in the classpath need to call it by setting the thread context class loader, Thread Context ClassLoader if not set is the default inherit the parent thread is the application class loader device

, Which is the parent class loader request to complete the sub-class of class loader loading operation.

3) OSGI modular hot deployment, custom class loader Bundle each program module has a class loader, even if Bundle will need to be replaced is replaced with the same load.

osgi class searches performed in the following order

 

Guess you like

Origin www.cnblogs.com/xiaofeiyang/p/11920846.html