当前位置:网站首页>Class loading mechanism and parental delegation mechanism
Class loading mechanism and parental delegation mechanism
2022-07-24 14:16:00 【Shanghai city flower】
One 、 Class loading
summary
1: When a program wants to use a class , Class is not loaded into memory , The system will initialize the class in three steps ( load - Connect - initialization )
2: Get the class loader :Class#getClassLoader()
3: Class loader (class loader) Used to load Java Class to Java In the virtual machine . Generally speaking ,Java Virtual machine usage Java Class as follows :Java Source program (.java file ) after Java After the compiler compiles, it is converted to Java Byte code (.class file ). Class loader is responsible for reading Java Byte code , Translates into java.lang.Class An instance of a class .
Any class , It needs to be established by the class loader that loads it and the class itself java Uniqueness in virtual machines .
1. load
- That is to say class File read into memory (JVM Method area ), And create a class object ( Member variables , Member method , Construction method ).
- Any class used by the system will create a class object .
2: Connect
- verification Whether there is correct internal structure , And coordinate with other classes
- Get ready Responsible for allocating memory for static members of a class ( Static is loaded as the class is loaded ), And set the default initialization value
- analysis Replace the symbol reference in the binary data of the class with a direct reference .
3: initialization
Class loading ( initialization ) opportunity
- Create an instance of a class
- Access static variables of the class or assign values to static variables
- Calling static methods of a class
- Use reflection methods to force the creation of a class or interface corresponding to java.lang.Class object
- Initializing a subclass of a class
- Use it directly java.exe Command to run a main class
Composition and classification of class loaders
- Class loading mechanism : Will be class File loaded into memory , And generate corresponding class object ,
- Root loader > Extend the classloader > system class loader
Root loader ( Boot class loader ) (Bootstrap classLoader)
1. be responsible for Java Loading of core classes ,, As part of the virtual machine , such as system.string, stay JDK Of jre Of lib In the catalog rt.jar In file 
2: Extend the classloader (Extension classLoader)
2. be responsible for jre Under the extended directory of jar Package loading , stay JDK in jre Of lib In the catalog ext Catalog

3: system class loader (system classLoader)
1. Responsible for jvm Load at startup from Java Ordered class file , as well as classpath Specified by environment variable jar Path of packages and classes , It includes everything jdk Class provided (rt-jar). Including classes written by developers and third-party jar package
2: load classpath Next class
3: Also called Application ClassLoader ,sun.misc.Launcher$AppClassLoader, It is System.getClassLoader() The return value of
4: Delegation mechanism for class loader
Class loader relationship
1: The upper leadership of the system class loader -----------> Extend the classloader
2: The upper leadership of the extension class loader -----------> Root loader
The order of execution of the delegation mechanism
- Created an object User user=new User();
- The system class loader found new User This class
- The system will call the automatic leader , Let the extension go to its own site to load user class
- Expansion will call their leaders , Let the root class loader go to its own site to load user class
1: The root class loader goes jre Under the rt.jar Go looking for A class
- If you find it , Just load , Then return user Object's class Object to the extension class loader , The extended class loader will this user.class The file is returned to the system class loader
- If not
2: The root class loader returns a null, Expansion will look for user class
- If the extension class is found user class , Just load , return user Object's class File to system classloader
- If not
3: The extension class is returned to the system class loader null, The system goes to its own territory ( Applications ) load A class
- If you find it , load , Go back to this class file , end
- If not , Throw out classNotFoundException Class is exception not found

5: Parent delegation model
summary
The process of parental delegation model : If a class loader receives a class load request , First, I won't load it myself , Instead, the request is sent to its own parent loader to complete , This is true of class loaders at every level . So all the load requests should eventually be sent to the top-level boot loader . Only when the parent class feedbacks that it cannot complete the load request , The subclass loader will try to complete it by itself

2: Break the parental delegation model
1: Three large-scale destruction of the model
- JDK 1.0->1.2 , loadClass() -> findClass()
- Model defects :JNDI service ,SPI The extension class is implemented by the manufacturer , It is impossible to recognize these classes when starting class loading . We have to introduce Thread context Class loading Thread Context ClassLoader. This kind of loader can pass setContextClassLoader() Set up , If the thread was created without setting , Will inherit from the parent thread . If it is not set in the global scope of the application , The default is AppClassLoader. With this ,JNDI The service can load the required SPI Extension code , That is, the parent class loader asks the child class loader to complete the class loading action . This actually violates the general principle of the parental delegation model , But there's nothing to do .
- The pursuit of program dynamics : “ Hot replacement ” , OSGI. JSR-291
2: Develop your own class loader
Class loading security mechanism
- The loading of classes will be from the root class loader --- Extend the classloader --- The system class loader goes down layer by layer , There will be no repeated loading
- Write a class by yourself Java.lang.string, Want to replace Java Root class library
1 Will not load , Because there are Java.lang.string. Will not load the classes written by yourself
6: Class loaders and threads
- Each thread has a separate class loader and cpu register ( Record the working state of the thread )
- getContextClassLoader: Get the classloader of the current thread

边栏推荐
- Stack and queue - 20. Valid parentheses
- About the flicker problem caused by using universalimageloader to load pictures and refresh data in recyclerview
- binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
- The fourth edition of probability and mathematical statistics of Zhejiang University proves that the absolute value of the correlation coefficient of random variables X and Y is less than 1, and some
- TypeError: 'str' object does not support item assignment
- Matlab program for natural gas flow calculation
- Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
- Nodejs uses the express framework to post the request message "badrequesterror:request aborted"
- Multithreaded common classes
- [untitled]
猜你喜欢

bibliometrix: 从千万篇论文中挖掘出最值得读的那一篇!

天然气潮流计算matlab程序

After reading this article, I found that my test cases were written in garbage

Bibliometrix: dig out the one worth reading from thousands of papers!

正则表达和绕过案例

小熊派 课程导读

RHCE first operation

Typo in static class property declarationeslint

Dialog manager Chapter 2: create frame window

Moving the mouse into select options will trigger the mouseleave event processing scheme
随机推荐
OWASP ZAP安全测试工具使用教程(高级)
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
2022.7.22 simulation match
RHCE first operation
Flink advanced features and new features (VIII) V2
Unity 委托 (Delegate) 的简单理解以及实现
MySQL community download address
After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
Lazy loading of pictures
Ansible installation and deployment of automated operation and maintenance
[oauth2] III. interpretation of oauth2 configuration
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
字符串——28. 实现 strStr()
Unity行人随机行走不碰撞
uni-app 背景音频 熄屏或者退回桌面之后不在播放
TypeError: 'str' object does not support item assignment
Introduction to Xiaoxiong school
Click event to create a new node
Detailed explanation of switch link aggregation [Huawei ENSP]
Stack and queue - 225. Implement stack with queue