JVM---class loader, custom class loader

JVM---custom class loader

1. Why do you need a custom classloader?

Application scenario introduction: Encryption prevents decompilation without JAVA class loader. Non-standard source loading code: bytecode is placed in the database, cloud, custom class loader, and source-specific loader.

2. Parent delegation model:

Check whether the class has been loaded by the current class loader. If it is not found, it will entrust the parent class loader to load it. If the startup class loader fails to load, use the extended class loader to try to load it.

 

Advantages: Avoid users writing their own classes to replace core classes, and avoid repeated loading of classes

 

3. Custom class loader

Inherit ClassLoader and implement the findClass method.

 

 

Java class loader mechanism summary

1. The organizational structure of the class loader, the class loader has a hierarchical structure, that is, the parent-child relationship

2. Classification:

The startup class loads the core class library of the virtual machine. Bootstrapclass loader.

       Standard extension class loader, which loads some extension classes in addition to the basic API. Extensionclass loader.

       Custom class loader to load application and programmer custom classes. AppClassLoader.

3. Class loading mechanism, classes are loaded into virtual machine memory including loading, linking, initialization, and linking is subdivided into verification, preparation, and parsing.

    3.1 Loading can be done using the class loader provided by the system, or it can be done by the user-defined class loader. The developer can control the byte stream through the class loader.

    3.2 Linking phase, verification, preparation, parsing. For class variables, the class method refers to the pointer to the method area, and for instantiated methods, the instance object is the offset.

    3.3 Initialization, use the new keyword to instantiate objects, access static fields and static methods of a class. When making a reflection call to a class, initialize the subclass, if the parent class has not been initialized, then initialize the parent class.

 

4. Three ways of class loading

A class instance created by the new keyword calls the Class.forName() method, loads the class through reflection, and calls the loadclass() method of a ClassLoader instance.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325587687&siteId=291194637