Introduction to the loading order of classes in java (ClassLoader)

1. ClassNotFoundExcetpion 
  In our development, we often encounter the exception of java.lang.ClassNotFoundExcetpion. Today I will summarize this problem. For this exception, it essentially involves class loading in the java technology system. Java's class loading mechanism is the core part of the technical system. Although it does not deal with us directly, a certain understanding of the mechanism behind it will help us to troubleshoot technical problems such as class loading failures in the program. 
2. Class loading process 
  A java file goes through a total of 5 stages from being loaded to being unloaded. The JVM divides the class loading process into: 
  loading -> linking (verification + preparation + parsing) -> initialization (using Preparation before) -> use -> unload 
(1) Loading 
  first obtains the binary byte stream of this class through the fully qualified name of a class; secondly, converts the static storage structure represented by this byte stream into the operation of the method area Time data structure; finally, a Class object representing this class is generated in the java heap as the access entry for these data in the method area. In general, it is to find and load the binary data of the class. 
(2) Link: 
  Verify: Ensure the correctness of the loaded class; 
  Prepare: Allocate memory for static variables of the class and initialize them to default values; 
  Parse: Convert symbolic references in the class to direct references; 
(3) For The static variable of the class is given the correct initial value 
3. Initialization of the class 
(1) When will the class be initialized 
  1) Create an instance of the class, that is, a new object 
  2) Access the static variables of a class or interface, or the Static variable assignment 
  3) Call the static method of the class 
  4) Reflection (Class.forName(“com.lyj.load”)) 
  5) Initialize a subclass of a class (the parent class of the subclass will be initialized first) 
  6) The startup class marked when the JVM starts, that is, the file name and class name The same class 
(2) The initialization sequence  of
  the class 1) If the class has not been loaded and linked, load and link first 
  2) If the class has a direct parent class, and the class has not been initialized (Note: in the In a class loader, a class can only be initialized once), then initialize the direct parent class (not applicable to interfaces) 
  3) If there are initialization statements (such as static variables and static blocks) in the added class, then execute these initialization statements in turn . 
  4) In general, the initialization order is: (static variable, static initialization block) –> (variable, initialization block) –> constructor; if there is a parent class, the order is: parent class static method –> subclass Static method –> Parent   class construction method
–  -> Subclass construction method 
The method area, and then create a java.lang.Class object of this class in the heap area to encapsulate the object of the class in the method area. Such as: 
这里写图片描述 
这里写图片描述

  类的加载的最终产品是位于堆区中的Class对象。Class对象封装了类在方法区内的数据结构,并且向Java程序员提供了访问方法区内的数据结构的接口。加载类的方式有以下几种: 
  1)从本地系统直接加载 
  2)通过网络下载.class文件 
  3)从zip,jar等归档文件中加载.class文件 
  4)从专有数据库中提取.class文件 
  5)将Java源文件动态编译为.class文件(服务器) 
5、加载器 
  JVM的类加载是通过ClassLoader及其子类来完成的,类的层次关系和加载顺序可以由下图来描述: 
   
这里写图片描述

(1)加载器介绍 
1)BootstrapClassLoader(启动类加载器) 
  负责加载$JAVA_HOME中jre/lib/rt.jar里所有的class,加载System.getProperty(“sun.boot.class.path”)所指定的路径或jar。 
2)ExtensionClassLoader(标准扩展类加载器) 
  负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包。载System.getProperty(“java.ext.dirs”)所指定的路径或jar。 
3)AppClassLoader(系统类加载器) 
  负责记载classpath中指定的jar包及目录中class 
4)CustomClassLoader(自定义加载器) 
  属于应用程序根据自身需要自定义的ClassLoader,如tomcat、jboss都会根据j2ee规范自行实现。

(2)类加载器的顺序 
1)加载过程中会先检查类是否被已加载,检查顺序是自底向上,从Custom ClassLoader到BootStrap ClassLoader逐层检查,只要某个classloader已加载就视为已加载此类,保证此类只所有ClassLoader加载一次。而加载的顺序是自顶向下,也就是由上层来逐层尝试加载此类。 
2)在加载类时,每个类加载器会将加载任务上交给其父,如果其父找不到,再由自己去加载。 
3)Bootstrap Loader(启动类加载器)是最顶级的类加载器了,其父加载器为null。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326553600&siteId=291194637
Recommended