当前位置:网站首页>委派双亲之类加载器
委派双亲之类加载器
2022-07-17 14:17:00 【西魏陶渊明】

作者: 西魏陶渊明
博客: https://blog.springlearn.cn/
西魏陶渊明
莫笑少年江湖梦,谁不少年梦江湖
- BootStrap ClassLoader:称为启动类加载器,是Java类加载层次中最顶层的类加载器,负责加载JDK中的核心类库,如:rt.jar、resources.jar、charsets.jar等,可通过如下程序获得该类加载器从哪些地方加载了相关的jar或class文件:
- Extension ClassLoader:称为扩展类加载器,负责加载Java的扩展类库,默认加载JAVA_HOME/jre/lib/ext/目下的所有jar。
- App ClassLoader:称为系统类加载器,负责加载应用程序classpath目录下的所有jar和class文件。
一、类加载器
类的加载在JVM的外部实现。对于任意的一个类,都必须由加载它的类加载器和这个类本身共同确立其在Java虚拟机中的唯一性。JVM提供中类加载器。
二、启动类加载器(Bootstrap ClassLoader)
负责加载 JAVA_HOME\lib 目录中的,或通过-Xbootclasspath 参数指定路径中的,且被 虚拟机认可(按文件名识别,如 rt.jar)的类。

三、扩展类加载器(Extension ClassLoader)
负责加载 JAVA_HOME\lib\ext 目录中的,或通过 java.ext.dirs 系统变量指定路径中的类 库。

四、应用程序类加载器(Application ClassLoader)
负责加载用户路径(classpath)上的类库。 JVM 通过双亲委派模型进行类的加载,当然我们也可以通过继承 java.lang.ClassLoader 实现自定义的类加载器。

五、原理解释
ClassLoader使用的是双亲委托模型来搜索类的,每个ClassLoader实例都有一个父类加载器的引用(不是继承的关系,是一个包含的关系),虚拟机内置的类加载器(Bootstrap ClassLoader)本身没有父类加载器,但可以用作其它ClassLoader实例的的父类加载器。当一个ClassLoader实例需要加载某个类时,它会试图亲自搜索某个类之前,先把这个任务委托给它的父类加载器,这个过程是由上至下依次检查的,首先由最顶层的类加载器Bootstrap ClassLoader试图加载,如果没加载到,则把任务转交给Extension ClassLoader试图加载,如果也没加载到,则转交给App ClassLoader 进行加载,如果它也没有加载得到的话,则返回给委托的发起者,由它到指定的文件系统或网络等URL中加载该类。如果它们都没有加载到这个类时,则抛出ClassNotFoundException异常。否则将这个找到的类生成一个类的定义,并将它加载到内存当中,最后返回这个类在内存中的Class实例对象。
六、为什么要使用双亲委托这种模型呢?
因为这样可以避免重复加载,当父亲已经加载了该类的时候,就没有必要子ClassLoader再加载一次。考虑到安全因素,我们试想一下,如果不使用这种委托模式,那我们就可以随时使用自定义的String来动态替代java核心api中定义的类型,这样会存在非常大的安全隐患,而双亲委托的方式,就可以避免这种情况,因为String已经在启动时就被引导类加载器(Bootstrcp ClassLoader)加载,所以用户自定义的ClassLoader永远也无法加载一个自己写的String,除非你改变JDK中ClassLoader搜索类的默认算法。
七、但是JVM在搜索类的时候,又是如何判定两个class是相同的呢?
JVM在判定两个class是否相同时,不仅要判断两个类名是否相同,而且要判断是否由同一个类加载器实例加载的。只有两者同时满足的情况下,JVM才认为这两个class是相同的。就算两个class是同一份class字节码,如果被两个不同的ClassLoader实例所加载,JVM也会认为它们是两个不同class。
/**
* rt目录:
* 加载rt.jar的类加载器
*/
ClassLoader rtClassLoader = StringBuffer.class.getClassLoader();
System.out.println(rtClassLoader);
/**
* lib/ext扩展包
* [email protected]
*/
ClassLoader extClassLoader = EventID.class.getClassLoader();
System.out.println(extClassLoader);
/**
* 当前应用加载器
* [email protected]
*/
ClassLoader classLoader = BaseSyntaxTest.class.getClassLoader();
System.out.println(classLoader);
/**
* [email protected]
*/
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println(currentClassLoader);
最后求关注,求订阅,谢谢你的阅读!
边栏推荐
- Win10 install Apache Jena 3.17
- ENVI_ Idl: use the inverse distance weight method to select the nearest n points for interpolation (bottom implementation) and output them to GeoTIFF format (the effect is equivalent to the inverse di
- Scala's dosing in idea
- (1) Learn about MySQL
- Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
- SAP S4 material management inventory module mard database table reading technical details
- The case has been solved --- no matter how to change the code from the logic of MQ consumption, it will not take effect
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- Introduction to virtualization troubleshooting
- 8.固定收益投资
猜你喜欢

The difference between journal log and oplog log

How much money can you make by inventing flash memory? This is a Japanese dog blood story

Svn learning

Win10安装Apache Jena 3.17

军品研制过程所需文件-进阶版

A fastandrobust volutionalneuralnetwork based defect detection model inproductqualitycontrol reading notes

Daily question brushing record (26)

每日刷题记录 (二十六)
![[in vivado middle note ILA IP core]](/img/df/0929363c12a337866d143e5b79439d.png)
[in vivado middle note ILA IP core]

Unity3d 读取mpu9250 例子原代码
随机推荐
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
LeetCode 2319. Judge whether the matrix is an X matrix
How to change and reset forgotten root password in RHEL 9
Journal日志与oplog日志的区别
Unity3d 模型中心点的转换(源代码)
A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-阅读笔记
Deep Learning for Generic Object Detection: A Survey-论文阅读笔记
Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
(二)使用MySQL
LOJ 2324 - "Tsinghua training 2017" small y and binary tree
Unity high version returned low version error
Set the interface language of CMD command prompt window to English
leetcode-08
CodeForces - 587E(线性基+线段树+差分)
After summarizing the surface based knowledge of the database
一个报错, Uncaught TypeError: ModalFactory is not a constructor
2022/7/16
Cmake common commands (V)
mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比
Antd drop-down multiple options to transfer values to the background for query operations