Java class loader--classloader

Source: Internet
Author: User

Java is designed primarily for the embedded domain, for some of the custom classes, consider using the load-on-demand principle, that is, when the program is used to load the class, saving memory consumption, the class loader can be loaded dynamically.

If you're just doing web development, you'll rarely have to deal with ClassLoader, but it's essential if you want to learn more about the architecture of the Tomcat server. The so-called ClassLoader, which is used to load Java classes into a Java Virtual machine, is responsible for reading the Java bytecode and converting it into an instance of the Java.lang.Class class so that the byte code. class file runs. The generic ClassLoader is responsible for locating the corresponding byte code based on a specified class, which is then defined as a Java class, and is also responsible for loading resources, including image files and configuration files.

The benefit of the ClassLoader in practical use is that it allows the Java class to load dynamically into the JVM and run, and then load the class as the program runs, providing a flexible way to load dynamically. For example, we are familiar with the applet, from the remote server to download the bytecode to the client dynamically loaded into the JVM can be run.

In Java's vast system, the system can be divided into three kinds of loaders, namely:

① startup ClassLoader (Bootstrap ClassLoader): The Loading object is the Java core Library, loading some key Java classes into the JVM, which is implemented using native code (c/s) Is not inherited Java.lang.ClassLoader, it is the final parent loader of all other classloader, is responsible for loading the <java_home>/jre/lib directory and the JVM-specified class library, in fact, it is part of the JVM as a whole, The JVM loads the specified classes into memory as soon as it is started, avoiding excessive I/O operations in the future and improving the efficiency of the system. Startup ClassLoader cannot be used directly by Java programs.

② Extension class loader (Extension ClassLoader): Loads an extension library of JAVA, that is, loads the classes inside the <java_home>/jre/lib/ext directory. This class is loaded by the bootstrap ClassLoader above, but since Bootstrap ClassLoader is not implemented in Java, it is out of the Java system, so if you try to call the extension ClassLoader's GetParent () The method gets the parent loader to get NULL, but its parent classloader is Bootstrap ClassLoader. The extension class loader can be used directly in Java.

③ Application ClassLoader (Application ClassLoader): Also known as the System class loader, which is responsible for loading the class library specified by the User class path (CLASSPATH), if the program does not define the ClassLoader itself, The Application class loader is used by default. It is also loaded by Bootstrap ClassLoader, but its parent load class is set to extension ClassLoader. If you want to use this loader, you can get it via Classloader.getsystemclassloader ().

If one day you want to write a class loader yourself, then you just have to inherit the Java.lang.ClassLoader class. So you can use the following figure 2-4-a clear representation of the various ClassLoader relationships, Bootstrap ClassLoader is the most fundamental class loader, which does not exist in the parent class loader, Extension ClassLoader by Bootstrap ClassLoader loaded, so its parent classloader is bootstrap classloader,application ClassLoader is also loaded by Bootstrap ClassLoader, But its parent loader is pointing to extension ClassLoader, and all other user-defined ClassLoader are loaded by application ClassLoader.

It can be seen that the more important the ClassLoader is loaded by the JVM, this is because security is taken into account because the loaded ClassLoader acts as the parent loader for the next class loader, ensuring security under the parental delegation model mechanism.

So what is a parental delegation model? For example, Grandpa, Dad, you three generations of only son, your family are genetically lazy genes, every time you encounter things to evade each other, now need a person to buy a packet of salt, or tonight's food will be colored tasteless, and you are the first to be entrusted to the supermarket to buy, but because of your laziness, you to your father, you can only promise , in the lazy drive, your father finally found an excuse to get a job, let your grandfather go out and walk around to buy a packet of salt, so your grandfather can only go to the supermarket to buy salt. The parent delegation model is similar to a mechanism in which the ClassLoader loads a class when it is first delegated to the parents of the ClassLoader, unless the parent ClassLoader cannot be loaded to load itself.

This model requires that, in addition to the top-level startup ClassLoader, the other classloader will have its own parent class loader. If a class is to be loaded, a classloader does not immediately attempt to load it, but instead delegates to the parent ClassLoader, the parent class loader receives and then attempts to delegate to its parent classloader, and so on, until it is delegated to the startup ClassLoader so that one layer at a level is delegated. The child loader tries to load itself only when the parent loader feedback itself is unable to complete the load. This mechanism ensures that Java applications use classes that are the same version of the Java Core Library. This mechanism also guarantees security, assuming that if application ClassLoader wants to load a destructive Java.lang.System class, the parent delegation model will be delegated to the bootstrap ClassLoader at the end of the level, Bootstrap ClassLoader checks that the class is already in the cache and does not load the destructive system class again.

In addition, the ClassLoader has an overall responsibility mechanism, that is, when a classloader loads a class, all other referenced classes that the class relies on are loaded by this classloader, unless another classloader is explicitly specified in the program.

Figure 2-4-Class 1 loader relationship

In Java, we identify a class with an exact match to the class name, that is, the package name and the class name. In the JVM, a class is uniquely identified by an exact match class name and an instance ID of a classloader. That is, the same virtual machine can have two package names, class names of the same class, as long as they are loaded by two different classloader. So when we often say in Java that two classes are not equal, it must be for the same class loader to load the premise of meaning, otherwise it is the same byte code, loaded by different ClassLoader, these two classes are not equal. This feature provides an isolation mechanism that is useful in a Tomcat server.

After understanding the various mechanisms of the JVM's ClassLoader, see how a class is loaded into the ClassLoader. When a class is ready to load, the ClassLoader first determines whether the class has been loaded (the loaded class is cached in memory) and returns the class directly if the class exists in the cache. Otherwise, gets the parent ClassLoader and, if the parent ClassLoader is NULL, is loaded by bootstrap ClassLoader and returns the class. If the parent loader is not NULL, it is loaded by the parent ClassLoader, the load succeeds returning class, the load failure is based on the classpath, the class file is found, and the class is loaded and the class is returned, and the ClassNotFoundException exception is thrown if it is not found.

Figure 2-4-Class 2 loading process

The ClassLoader is a JVM-level design, and we don't often deal with him at all, but if you want to understand how the entire Java system works, if you're designing your own framework or middleware or a larger Java software, you have to be familiar with the mechanics of the ClassLoader, and in real-world design, Based on the actual situation, the class loader can be used to provide isolation and sharing of class libraries, ensuring that your software's different levels of logic splitting programs do not affect each other and provide better security.

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.