当前位置:网站首页>Parental delegation mechanism

Parental delegation mechanism

2022-06-24 22:50:00 Swarford

Every time ⼀ Both classes have ⼀ A corresponding ClassLoader Class loader , The classloader is in ⼯ It will default to ⽤ Parent delegate mechanism ;
ClassLoader The specific role of will be class The bytecode file is loaded into jvm Go to the virtual machine , The program will run correctly ;
however ,jvm Starting does not load all at once class file , It's loading dynamically as needed ;

The so-called parental delegation mechanism , It's called ClassLoader Of loadClass( ) The rules of :
 Insert picture description here

The specific process :

  1. The first step is the application class loader , call findLoadedClass( ) Check whether this class loader has loaded this class ,
  2. If not , Find out if there is parent The superior , If there is a superior, the superior's loadclass( ) Continue to find , It's like recursion
  3. until parent The superior is null( Start class loader C++), Delegate the start class loader to call findBootstrapClassOrNull( ) stay jre/lib In the catalog jar Start looking for classes in the package
  4. If the boot class loader is not found , Will report a mistake ClassNotFoundException, Expand the class loader catch Exceptions are caught , Just before the extension class loader calls findClass()( Rewritten per loader ) Look up the directory jre/lib/ext;
    If not , Will report a mistake ClassNotFoundException, Application class loader's catch Exceptions are caught , Finally, call the application class loader's own findClass( ) To classpath Find the class under the directory

 Insert picture description here
 Insert picture description here

Why check from bottom to top ?

To avoid repeated loading ;

In some cases, the parental delegation mechanism will be broken :

JDBC Of DriverManager By default, it is loaded by the boot class loader , But there is no... In the boot class loader Mysql Drive class , So the call to be displayed Class Of forName Method uses an application class loader to load Mysql Driver class
 Insert picture description here

原网站

版权声明
本文为[Swarford]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241650112530.html