Classloader, urlclassloader, dynamic loading of classes!

Source: Internet
Author: User

Instance SVN:

SVN: // www.oksvn.com/classloader/classloader

SVN: // www.oksvn.com/classloader/classloaderweb

 

In particular, through urlclassloader, in combination with JMX management for projects such as J2EE, It is very convenient to implement dynamic modification of projects. For example, we can flexibly add and modify filters in Mina, update and delete the filter !!!

 

(1) JVM loading process and loading principle
The so-called loading is the process of searching for the binary form of a class or an interface and using the binary form to construct the class object representing this class or this interface,
The class or interface name is given. Of course, the name can also be calculated, but it is more common to searchSource codeAfter compilation by the compiler
. In Java, the Class Loader loads a class into a Java Virtual Machine. Three steps are required: Loading, linking, and initialization,
The link can be divided into three steps: verification, preparation, and resolution. In addition to resolution, the other steps are completed in strict order. The main work of each step is as follows:

Load: Query and import binary data of classes or interfaces;
Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional;
Verify: Check whether the binary data of the import class or interface is correct;
Preparation: allocate and initialize the storage space for static variables of the class;
Resolution: convert a symbolic reference to a direct reference;
Initialization: initialization of static variables of the activation class JavaCodeAnd static Java code blocks.

(2): What is the class in Java?
A class indicates the code to be executed, and data indicates its related status. The status changes frequently, but the Code does not. When we compare a specific state with a class, it means to make a class case. Although instances of the same class have different statuses, they all correspond to the same code. In Java, a class usually has a. Class file, but there are exceptions. In the Java Runtime Environment (Java runtime), each class has a Java object with the first class as the code that appears. It is Java. lang. class instance. We compile a Java file, and the compiler will embed a public, static,
The final modifier is of the Java. Lang. class type, and the domain variable named class is in its bytecode file. Because the public modifier is used, we can access it in the following form:
Java. Lang. Class Klass = myclass. Class;
Once a class is loaded into JVM, the same class will not be loaded again (Remember, the same class ). There is a problem here: what is "the same class "? Just as an object has a specific State, that is, identifier, an object is always associated with its code (class. Similarly, the classes loaded into JVM also have a specific Identifier. Let's take a look.
In Java, a class uses its fully-matched Class Name (fully qualified class name) as its identifier. Here, the fully-matched class name includes the package name and class name. However, in JVM, a class uses its full name and an instance that loads classloader as a unique identifier. Therefore, if a package named PG contains a class named Cl that is loaded by an instance kl1 of the Class Loader klassloader, that is, c1.class is expressed as (CL, PG, kl1) in JVM ). This means that the instances (CL, PG, kl1) and (CL, PG, kl2) of the two class loaders are different, so the classes loaded by them are completely different, incompatible. So how many types of loaders are there in the JVM? In the next section, we will reveal the answer.

(3): Java classloader:
In Java, we can obtain the following three classloader classes:
I. Basic concepts of classloader

1. classloader category

The class loader is used to load a class into JVM.

The JVM Specification defines two types of class loaders: Start the internal loader (bootstrap) and user-defined loader (user-defined class loader ).

At runtime, the JVM generates three classloader: Bootstrap classloader, extension classloader, and appclassloader. Bootstrap is written in C ++. We cannot see it in Java. It is null. It is a class loader that comes with JVM and is used to load core class libraries, such as Java. Lang.

The parent of appclassloader is extclassloader, while the parent of extclassloader is Bootstrap classloader.

 

Java provides the abstract class classloader. All user-defined class loaders instantiate the class of the class loader. System class loader is a special user-defined class loader provided by the JVM implementer. It is loaded by default when the programmer does not specify a loader. The system class loader can be obtained through the classloader. getsystemclassloader () method.

 

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.