class loader and Parent delegation model

Source: Internet
Author: User

The ClassLoader is divided into the following three types, from the top to the bottom level .
(1) Start class loader (Bootstrap ClassLoader)
This classloader is responsible for loading the class library that is stored under java_home/lib or in the path specified by the-xbootclasspath parameter into the virtual machine memory. The startup ClassLoader cannot be referenced directly by a Java program.
(2) Extension class loader (Extension ClassLoader)
The loader is responsible for loading all class libraries in the Java_home/lib/ext directory, or in the path specified by the Java.ext.dirs system variable, and developers can use the extension ClassLoader directly
(3) Application class loader (application ClassLoader)
This loader is the return value of the Getsystemclassloader () method in ClassLoader, so it is generally referred to as the system ClassLoader. It is responsible for loading the class library specified on the user classpath (Classpath), which can be used directly, if the application does not customize its own classloader, typically this is the default class loader in the program.

 class-Loaded parental delegation model
The parental delegation model requires that, in addition to the top-level startup class loader, other classloader should have their own parent ClassLoader. The parent-child relationship between ClassLoader is generally not implemented as an inheritance relationship, but instead uses the code of the parent loader with the combined relationship.
Working process:
If a class loader receives a request for a class load, it first does not attempt to load the class itself, but instead delegates the request to the parent class loader, which is the case for each level of the ClassLoader, so all load requests should eventually be passed to the top-level startup ClassLoader.
The child loader tries to load itself only when the parent loader has feedback that it cannot complete the request (it does not find the desired class in its search scope).
Benefits:
The Java class has a hierarchical relationship with precedence as its classloader. For example, the class object, which is placed in Rt.jar, regardless of which class loader is loading the class, is ultimately delegated to the startup ClassLoader for loading, so the object class is the same class in the various ClassLoader environments of the program
Judging whether the two classes are the same is done by classloader.class, so even if the same class file is loaded by two classloader, they are also different classes

Implement your own loader
You only need to inherit ClassLoader and overwrite the Findclass method.
When the LoadClass method is called, it is loaded in the parent loader based on the delegation model, and if the load fails, its own Findclass method is called to complete the load
Reference: http://blog.csdn.net/altchen/article/details/328698

how the class loader recognizes
By Findloadedclass to identify whether a class has been loaded, this method is a wrapper class for FindLoadedClass0, and FindLoadedClass0 is a native method

Excerpt from: http://wangwengcn.iteye.com/blog/1618337

class loader and Parent delegation model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.