Class-Loaded Father delegation mechanism

Source: Internet
Author: User

we all know that. The class loader is used to load classes intoJavavirtual machines. FromJDK2.0at first, the process of loading a class takes a father-commissioned mechanism. JVMof theClassLoaderthe use of a tree-shaped structure, in additionoutside of the root class loader, eachClassLoaderwill haveand there is only one parent class loader, the user-definedClassLoaderthe defaultThe parent class loader is the system ClassLoader, and of course you can specify that you need to use aClassLoaderexample, let's see him.Our father-child relationship:

in the parent class delegate mechanism, when a JavaProgram Request LoaderLoader1LoadingHelloclass,Loader1first delegate your own father loader to loadHelloclass, if the Father Loader can load, then the loader finishes loading the characters, otherwise the loaderLoader1itself loadedHelloclass. Let's look at it again.Javathe virtual machine comes with several loaders:

exceptJavain addition to the loader that comes with the virtual machine, our users can customize their own classloader, depending on their needs. Javaprovides an abstract classJava.lang.ClassLoder, all user-defined class loaders inherit thisClassLoaderclass.

Note: The parent-child relationship between the loaders actually refers to the wrapper relationship between the loader objects, not the inheritance relationship between the classes. A pair of parent-child loaders may be two instances of the same loader class, or not. A parent loader object is wrapped in a child loader object. When a custom class loader instance is generated, the system ClassLoader becomes the parent loader for the ClassLoader if it is not specified by its parent loader. If the parent class loader is specified in the constructor method, then the parent ClassLoader is the specified loader. The proof is as follows:


ClassLoader loader1 = new Myclassloader ();//parameter Loader1 will act as the Loader2 's parent loader ClassLoader loader2 = new Myclassloader (loader1);

when Java when the virtual machines load a class, exactly which classloader to load? ? We look at:

Loader1and theLoader2is the two class loader we define ourselves,Loader1and theLoader2is a parent-child relationship. Now we want to makeLoader2This class loader loads one of our own writtenSampleclass:Loader2.loadclass("Sample"), let's analyze to see which class loader to load. When this code is executed,Loader2first go to your own namespace to findSampleclass has been loaded and returned directly to the class if it is loaded .classthe reference to the object. IfSampleclass has not yet been loaded,Loader2First RequestLoader1load on behalf ofLoader1then request the System class loader to load, the system class loader then requests the Extension class loader, the extension class loader requests the root class loader again, if the root class loader and the extension class loader cannot load, the System class loader attempts to load, if it can be loaded, it willSampleclass that corresponds to theClassobject is returned to theLoader1,Loader1when you return a reference toLoader2, which will successfullySampleclass is loaded into the virtual machine. If the system ClassLoader does not loadSampleclass, youLoader1try to loadSampleOh, ifLoader1cannot be loaded, theLoader2attempt, if all class loads are not loaded, throwclassnotfoundexceptionexception.

define the ClassLoader : If a class loader can load a class, then the ClassLoader is called: Defining the ClassLoader, and defining the class loader and all of its sub-loaders are called the initial ClassLoader .


The advantage of the parent delegation mechanism is that it can improve the security of the software system. Because under the word mechanism, a user-defined ClassLoader cannot load a reliable class that should be loaded by the parent loader, preventing unreliable malicious code from replacing a reliable class loaded by the parent ClassLoader, thereby preventing unreliable and even malicious code from being loaded by the parent class loader. For example, the Java.lang.Object class is always loaded by the root ClassLoader, and no other user-defined class loader can load the Java.lang.Object class that contains malicious code.


Namespace , in fact the namespace we're talking about here is our a common package in Java, each classloader has its own namespace, and the namespace consists of classes loaded by the loader and all the parent loaders. In the same namespace, the full name of the class (including the package name of the Class) does not appear in the same two lei; in different namespaces, it is possible that the full name of the class (including the package name of the Class) is the same as two classes.


Classes that are loaded by the same class loader that belong to the same package make up a run-time package. Decide whether the two classes belong to the same run-time package, not only to see if their package names are the same, but also to see if they define the same classloader. Only classes and members that are visible (the default access level) can be accessed between classes that belong to the same run-time package. Suppose the user has customized a class java.lang.TestCase and is loaded by the class loader for the custom, because the Java.lang.TestCase and core class libraries java.lang.* loaded by different classloader, they belong to different runtime packages, Therefore, Java.lang.TestCase cannot access the package visible members in the core library Java.lang package.

Class-Loaded Father delegation mechanism

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.