当前位置:网站首页>Delegate parents and other loaders
Delegate parents and other loaders
2022-07-19 11:17:00 【TaoYuanming of the Western Wei Dynasty】

author : TaoYuanming of the Western Wei Dynasty
Blog : https://blog.springlearn.cn/
TaoYuanming of the Western Wei Dynasty
Don't laugh at the young Jianghu dream , Who doesn't dream of Jianghu
- BootStrap ClassLoader: It's called the boot class loader , yes Java The top-level classloader in the class loading hierarchy , Responsible for loading JDK Core class library in , Such as :rt.jar、resources.jar、charsets.jar etc. , The following procedure can be used to obtain where the class loader loads the relevant jar or class file :
- Extension ClassLoader: It's called an extended class loader , Responsible for loading Java The extended class library of , The default load JAVA_HOME/jre/lib/ext/ All of this jar.
- App ClassLoader: It's called a system class loader , Responsible for loading the application classpath All under directory jar and class file .
One 、 Class loader
Class is loaded in JVM External implementation of . For any class , Must be established by the class loader that loads it and the class itself Java Uniqueness in virtual machines .JVM Provide medium class loader .
Two 、 Start class loader (Bootstrap ClassLoader)
Responsible for loading JAVA_HOME\lib In the directory , Or through -Xbootclasspath Parameter specifies... In the path , And be Virtual machine approval ( Identify by file name , Such as rt.jar) Class .

3、 ... and 、 Extend the classloader (Extension ClassLoader)
Responsible for loading JAVA_HOME\lib\ext In the directory , Or through java.ext.dirs The system variable specifies the class in the path library .

Four 、 Application class loader (Application ClassLoader)
Responsible for loading user paths (classpath) Class library . JVM Load the class through the parent delegation model , Of course, we can also inherit java.lang.ClassLoader Implement custom class loader .

5、 ... and 、 Explanation of principle
ClassLoader The parent delegation model is used to search for the properties of classes , Every ClassLoader Instance has a reference to the parent loader ( Not an inherited relationship , It's an inclusive relationship ), Virtual machine built-in classloader (Bootstrap ClassLoader) There is no parent loader in itself , But it can be used for other purposes ClassLoader Instance's parent loader . When one ClassLoader When an instance needs to load a class , It will try to search for a class in person before , Delegate this task to its parent loader first , This process is checked from top to bottom , First, the top-level class loader Bootstrap ClassLoader Attempt to load , If it's not loaded into , Then transfer the task to Extension ClassLoader Attempt to load , If it doesn't load into , Is passed on to App ClassLoader Loading , If it doesn't load either , Is returned to the originator of the delegate , From it to the specified file system or network, etc URL Load the class in . If none of them are loaded into this class , Throw out ClassNotFoundException abnormal . Otherwise, the found class will be generated into a class definition , And load it into memory , Finally, return the in memory Class Instance object .
6、 ... and 、 Why use a parent delegation model ?
Because this can avoid repeated loading , When the father has loaded the class , There is no need ClassLoader Load again . Considering the safety factor , Let's think about , If you don't use this delegation mode , Then we can use the customized String To dynamically replace java The core api Type defined in , There will be a very big security risk , And the way parents entrust , This can be avoided , because String Already booted class loader at boot time (Bootstrcp ClassLoader) load , So user defined ClassLoader Never load a self written String, Unless you change JDK in ClassLoader Default algorithm for search class .
7、 ... and 、 however JVM When searching for classes , How to judge two class It's the same ?
JVM In judging two class Is it the same , It's not just about judging whether two class names are the same , And to determine whether it is loaded by the same classloader instance . Only when both are satisfied ,JVM I think these two class It's the same . Even two class It's the same class Bytecode , If two different ClassLoader Instance loaded ,JVM I think they are two different class.
/**
* rt Catalog :
* load rt.jar Class loader for
*/
ClassLoader rtClassLoader = StringBuffer.class.getClassLoader();
System.out.println(rtClassLoader);
/**
* lib/ext Expansion pack,
* [email protected]
*/
ClassLoader extClassLoader = EventID.class.getClassLoader();
System.out.println(extClassLoader);
/**
* Current Application Loader
* [email protected]
*/
ClassLoader classLoader = BaseSyntaxTest.class.getClassLoader();
System.out.println(classLoader);
/**
* [email protected]
*/
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println(currentClassLoader);
Finally, please pay attention , Request subscription , Thanks for reading !
边栏推荐
- Loj#2324-「清华集训 2017」小 Y 和二叉树
- Develop the first Flink app
- Unity high version returned low version error
- Model comparison of material inventory management between sap ECC and s4hana material
- Introduction to the universal theme system of SAP appgyver
- 玩转CANN目标检测与识别一站式方案
- Accident caused by the combination of annotation transaction + distributed lock at the read committed level -- disorder of activity purchase opportunities
- 2022/7/15
- Antd drop-down multiple options to transfer values to the background for query operations
- Category imbalance in classification tasks
猜你喜欢

Download of common getshell tools

Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)

剑指 Offer II 041. 滑动窗口的平均值

Scala's dosing in idea

How to change and reset forgotten root password in RHEL 9

Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记

今日睡眠质量记录79分
![Some methods of early MCU encryption [get data in the comment area]](/img/14/8e1dcb799d8a3c0aefcac09be9dc51.png)
Some methods of early MCU encryption [get data in the comment area]

PowerCLI 脚本性能优化

Discussion on Euler angle solution of rocket large maneuvering motion
随机推荐
Google Earth engine app (GEE) - set up a nighttime lighting timing analysis app in China
LeetCode 745. Prefix and suffix search
Google Earth engine - Hansen global forest change v1.8 (2000-2020) forest coverage and forest loss data set
The concept of data guard broker and the configuration process of data guard broker
Second classification learning is extended to multi classification learning
Deep learning for generic object detection: a survey
Introduction to virtualization troubleshooting
web安全入门-部署Snort开源IDS/IPS系统
玩转CANN目标检测与识别一站式方案
UE4 understanding of animation blueprint
A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記
Category imbalance in classification tasks
Powercli script performance optimization
Cmake常用命令(五)
Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries
Qt 两个重载QListWidget控件对象实现selectitem drag拖拽
Satellite network capacity improvement method based on network coding
2022/7/14
leetcode-08
SPI服务发现机制