1 . What is class loading
The loading of a class refers to reading the binary data in the class's. class file into memory , placing it in the method area of the run-time data area, and then in the heap area Creates a Java.lang.Class object that encapsulates the data structure of the class within the method area. The final product loaded by the class is a class object located in the heap, which encapsulates the data structure of the class within the method area, and provides the Java programmer with an interface to access the data structures within the method area.
2, the life cycle of the class
the process of loading , validating, preparing, parsing, and initializing five stages of the class load . In these five phases, the order in which the four phases of loading, validating, preparing, and initializing occurs is deterministic, and the parsing phase is not necessarily, which in some cases can begin after the initialization phase, in order to support runtime bindings for the Java language (also become dynamic or late bound). Also note that the stages here are started in order, rather than sequentially or in order, because these phases are usually mixed in a cross-section, often invoking or activating another phase during one phase of execution .
3. class loader
From the point of view of a Java virtual machine, there are only two different classloader: start the ClassLoader : it uses C + + implementations (this is limited to hotspots, That is, the default virtual machine after JDK1.5, there are many other virtual machines are implemented in the Java language, is a part of the virtual machine itself; all other ClassLoader: These classloader are implemented by the Java language, independent of the virtual machine, and all inherit from the abstract class Java.lang.ClassLoader, which The class loader needs to be loaded into memory by the startup ClassLoader before it can load other classes.
From the Java Developer's point of view, the class loader can be roughly divided into the following three categories:
start the ClassLoader : Bootstrap ClassLoader, which is responsible for loading the jdk\jre\lib (the JDK represents the JDK's installation directory, the same below), or the path specified by the-xbootclasspath parameter, and class libraries that can be recognized by the virtual machine (such as Rt.jar, all classes beginning with java.* are bootstrap ClassLoader loaded). The startup ClassLoader cannot be referenced directly by a Java program.
Extension class loader : Extension ClassLoader, which is implemented by Sun.misc.launcher$extclassloader, is responsible for loading the Dk\jre\lib\ext directory, or all class libraries in the path specified by the JAVA.EXT.DIRS system variable (such as classes beginning with javax.*), developers can use the extension class loader directly.
Application ClassLoader : Application ClassLoader, which is implemented by Sun.misc.launcher$appclassloader, is responsible for loading the class specified by the user class path (ClassPath). Developers can use the ClassLoader directly, if the application does not customize its own classloader, typically this is the default class loader in the program.
Applications are loaded with each other by these three kinds of loaders, and if necessary, we can also add a custom class loader. Because the JVM's own ClassLoader only knows how to load the standard Java class file from the local file system, if you write your own classloader, you can do the following:
1) The digital signature is automatically validated before the non-confidence code is executed.
2) dynamically create custom build classes that meet user-specific needs.
3) Obtain Java class from a specific location, such as in a database and in a network.
JVM class loading mechanism
? overall , when a classloader is responsible for loading a class, other classes that the class relies on and references will also be loaded by the ClassLoader, unless the display uses a different classloader to load
? The parent class Delegate , which first lets the parent ClassLoader attempt to load the class, attempts to load the class from its own classpath only if the parent class loader cannot load the class
? caching mechanism, the caching mechanism will ensure that all the loaded class will be cached, when the program needs to use a class, the class loader first look for the class from the buffer, only the buffer does not exist, the system will read the corresponding binary data, and convert it into a class object and store it in a buffer. This is why the JVM must be restarted after the class has been modified, and the program's modifications will not take effect
4 . Loading of Classes
There are three ways to load a class:
1. When the command line starts the application, it is loaded by the JVM initialization
2. Dynamic loading via Class.forName () method
3. Dynamic loading via Classloader.loadclass () method
5. Parental assignment Model
The workflow of the parent delegation model is that if a classloader receives a request for a class load, it does not attempt to load the class on its own, but instead delegates the request to the parent loader to complete, then up, so that all class load requests should eventually be passed to the top-level startup class loader. The load cannot be completed until the parent loader finds the required class in its search scope, and the child loader tries to load the class on its own.
Parent delegation Mechanism:
1. When Appclassloader loads a class, it first does not attempt to load the class itself, but instead delegates the class load request to the parent ClassLoader Extclassloader to complete.
2. When Extclassloader loads a class, it does not attempt to load the class on its own, but instead delegates the class load request to Bootstrapclassloader to complete.
3, if the Bootstrapclassloader load failure (for example, in $java_home/jre/lib not found in the class), will use the Extclassloader to try to load;
4, if the Extclassloader also failed to load, the Appclassloader will be used to load, if the Appclassloader also failed to load, will report an exception classnotfoundexception.
6. Custom class Loader
Typically, we use the System class loader directly. However, sometimes we also need to customize the ClassLoader. For example, the application is to transfer the Java class byte code through the network, in order to guarantee the security, these bytecode has been encrypted processing, then the system ClassLoader cannot load it, so it needs to be implemented by the custom class loader. Custom class loaders are generally inherited from the ClassLoader class, from the above analysis of the LoadClass method, we only need to rewrite the Findclass method.
The core of the custom ClassLoader is the acquisition of the bytecode file, and if it is an encrypted bytecode, the file must be decrypted in that class. Since this is just a demonstration, I did not encrypt the class file, so there is no decryption process. Here are a few things to note:
1, the file name passed here needs to be the full-qualified name of the class, that is, the com.paddx.test.classloading.Test format, because the DefineClass method is processed in this format.
2, it is best not to rewrite the LoadClass method, because it is easy to destroy the parental delegation mode.
3, this kind of test class itself can be loaded by the Appclassloader class, so we can not put Com/paddx/test/classloading/test.class under the classpath. Otherwise, due to the existence of the parent delegation mechanism, the class is directly loaded by Appclassloader and not by our custom class loader.
Http://www.cnblogs.com/ityouknow/p/5603287.html
Java class loading mechanism