当前位置:网站首页>双亲委派机制
双亲委派机制
2022-06-24 19:39:00 【斯沃福德】
每⼀个类都有⼀个对应的ClassLoader类加载器,类加载器在⼯作时会默认使⽤双亲委派机制;
ClassLoader的具体作用就是将class字节码文件加载到jvm虚拟机中去,程序就可以正确运行了;
但是,jvm启动时并不会一次性加载所有的class文件,而是根据需要去动态加载;
所谓双亲委派机制,就是调用ClassLoader的loadClass( ) 的规则:
具体过程:
- 首先进入的是应用类加载器,调用findLoadedClass( ) 检查本类加载器是否加载过这个类,
- 如果没找到,找是否有parent上级,有上级则调用上级的 loadclass( ) 继续查找,类似递归
- 直到parent上级为null(启动类加载器C++),则委派启动类加载器调用findBootstrapClassOrNull( ) 在jre/lib 目录下的jar包中开始查找类
- 如果启动类加载器没找到,会报错
ClassNotFoundException,拓展类加载器的catch会捕捉到异常,就在扩展类加载器调用findClass()(每个加载器重写过) 查找目录jre/lib/ext;
如果还没有,会报错ClassNotFoundException,应用类加载器的catch会捕捉到异常,最后调用应用类加载器自己的findClass( ) 到classpath 目录下找类


为什么要从下到上检查?
为了避免重复加载;
某些情况下会打破双亲委派机制:
JDBC的DriverManager默认是由启动类加载器加载,但启动类加载器中没有Mysql驱动类, 于是要显示的调用Class的forName方法使用一个能应用类加载器来加载Mysql的驱动类
边栏推荐
猜你喜欢

软件设计的七大原则
How to solve the problem that the computer suddenly can't connect to WiFi

网上立案流程

Industrial development status of virtual human

开发规范~参数校验异常、异常返回提示切面

Kubevela v1.2 release: the graphical operation console velaux you want is finally here

MySQL + JSON = King fried!!

Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration

NIO多路复用之Selector的使用

Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
随机推荐
Concurrency of heap memory allocation
Leetcode: push domino (domino simulation)
DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别
进程的通信方式
Chapter 10 project communication management
Docker 安装 Redis-5.0.12,详细步骤
A girl has been making hardware for ten years. 。。
Online filing process
Power system | IEEE paper submission process
FANUC机器人_KAREL编程入门学习(1)
Structure du disque
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
find your present (2)
Problem solving - nested lists
Data center basic network platform
ThreadLocal local thread
NiO, bio, AIO
Layer 2 and layer 3 forwarding principle based on VLAN
See how sparksql supports enterprise level data warehouse
Information update on automatic control principle