Principles of Java class loaders

Source: Internet
Author: User

 

Reprinted from http://blog.csdn.net/yuekun1172006/archive/2007/06/02/1634878.aspx

The class loader is a JVM class used to load classes. It is a very important concept for Java programming. Generally, programmers can ignore the existence of class loaders when writing programs. However, for programming on the server side or in some special cases, it is necessary to have a deep understanding of the Class Loader mechanism and its implementation under different circumstances.

First, when a JVM is started, Java starts to use three class loaders by default. They are:

  • Bootstrap class loader;

  • Extension class loader;

  • System class loader;

They implement the following functions:

  • The bootstrap loader is a Class Loader implemented using local code. It is responsible<Java_Runtime_Home>/lib
    The following class library is loaded into the memory.

  • The extended class loader is implemented by Sun's extclassloader. It is responsible< Java_Runtime_Home >/lib/ext
    Or, the class library specified by the system variable java. Ext. DIR is loaded into the memory.

  • The system class loader is also called the application class loader, which is implemented by Sun's appclassloader. It is responsible for loading the class library specified in the system classpath to the memory.

When
How does the Class Loader Work when an application needs to load a class into the memory? This design is an important aspect of the Class Loader: the proxy mechanism. In addition to the bootstrap loader,
Each has a parent loader. For the three default class loaders defined by the system, the bootstrap loader is the parent class loader of the extended class loaders, the extended class loader is the parent class loader of the system class loader. Of course, the application
Programs can also use their own class loaders to load classes using specific methods. Therefore, the class loaders in the entire system form a tree structure.

When a class loader is used to load a class, the Class Loader first uses its parent class loader to load the class. For each loaded class, JVM assigns a unique ID to it. Therefore, different class loaders can load the same class to the JVM. For exampleClassLoaderA
AndClassLoaderB
:



Figure 1 Structure of the Class Loader




Hypothesis classC
In the class path specified by the system loaderClassLoaderA
Still useClassLoaderB
, Only get the same classC
.

However, ifC
InClassLoaderA
AndClassLoaderB
In the specified class library, useClassLoaderA
ClassC
The instance is different fromClassLoaderB
ClassC
Instance. Although the two class loaders are in the same JVM.

The upward proxy structure of the above class loader looks perfect, but it still seems useless when the system becomes complicated.

Example
For example, after Java introduces JNDI, the core part of JNDI is loaded into JVM at JVM startup through the bootstrap loader. And jdni
The core part is to load the specific classes defined in the user's class path at runtime through configuration information to fulfill specific needs. This is not supported by the upward proxy mode of the Class Loader defined above.

To solve this problem, Java 2 introduces the thread context class loader. Each thread has a context class loader. The context class loader uses the following method:Thread.setContextClassLoader()
If the current thread does not call this method after creation to set the context class loader, the current thread inherits the context Class Loader from its parent thread. If the context class loader is not set for the entire application, the system class loader is set as the context class loader for all threads.

Pair
As described above, the JNDI core class loaded by the bootstrap loader uses the context class loader to load the required JNDI
The implementation class, instead of proxy the load task to its parent class loader. In this way, the above problem is solved. The context class loader can be considered in the traditional Java
A backdoor is opened to the proxy mechanism. The context class loader is widely used in J2EE, such as Java Naming Service (JNDI) and Java API
For XML parsing (JAXP) (Note: In java1.4, JAXP is used only as part of the Java core class.
The context class loader to load different implementation classes.

In simple terms, the class loaders in Java are the above types. However, there are still many changes in actual use. We will explain some situations respectively.

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.