Diagram ClassLoader The process of loading class and customizing ClassLoader
Blog Category:
JAVAJVM Virtual Machine Extsun
/**
* Reprint Please indicate author Longdick http://longdick.iteye.com
*
*/
Different classes in the Java application environment are loaded by different classloader.
The default ClassLoader in a JVM are bootstrap ClassLoader, Extension ClassLoader, App ClassLoader, respectively:
- Bootstrap ClassLoader is responsible for loading Java base classes, mainly Rt.jar, Resources.jar, Charsets.jar, and class in the%jre_home/lib/directory
- Extension ClassLoader is responsible for loading the Java extension classes, primarily the jar and class in the%jre_home/lib/ext directory
- APP ClassLoader is responsible for loading all classes in the classpath of the current Java application.
Where Bootstrap ClassLoader is the JVM level, written by C + +, Extension ClassLoader, App ClassLoader are all Java classes, inherited from URLClassLoader superclass.
Bootstrap ClassLoader is initiated by the JVM, then initializes sun.misc.Launcher, sun.misc.Launcher initializes extension ClassLoader, App ClassLoader.
is the ClassLoader load class flowchart to load a class of procedure class examples that illustrate the entire classloader process.
Bootstrap ClassLoader, Extension ClassLoader, App ClassLoader The relationship between the following:
Bootstrap ClassLoader is Extension classloader parent,extension ClassLoader is the parent of app ClassLoader.
But this is not an inheritance relationship, just a semantic definition, basically, every ClassLoader implementation, there is a parent ClassLoader.
The parent of the current ClassLoader can be obtained through the ClassLoader GetParent method. Bootstrap ClassLoader is special because it is not a Java class, so the GetParent method of extension ClassLoader returns NULL.
After understanding the principles and processes of classloader, we can try custom ClassLoader.
About Custom ClassLoader:
Due to some special requirements, we may need to customize the load behavior of ClassLoader, then we need to customize the ClassLoader.
The custom ClassLoader needs to inherit the ClassLoader abstract class, overriding the Findclass method, which defines how ClassLoader finds the class.
The main methods that can be extended are:
Findclass define how to find class
DefineClass loading class file bytecode as class in the JVM
FindResource define how resources are found
If we have trouble, we can use or inherit the existing ClassLoader implementations, such as
java.net.URLClassLoader
java.security.SecureClassLoader
java.rmi.server.RMIClassLoader
sun.applet.AppletClassLoader
Extension ClassLoader and App ClassLoader are all subcategories of java.net.URLClassLoader.
This is the construction method of URLClassLoader:
Public URLClassLoader (url[] URLs, ClassLoader parent)
Public URLClassLoader (url[] URLs)
The URLs parameter is an array of classpath URLs that need to be loaded, and you can specify the parent ClassLoader, which defaults to the ClassLoader of the currently calling class as the parent.
code example:
Java code
- ClassLoader ClassLoader = new URLClassLoader (URLs);
- Thread.CurrentThread (). Setcontextclassloader (ClassLoader);
- Class Clazz=classloader.loadclass ("Com.company.MyClass"); Use the LoadClass method to load the class, which is below the classpath specified in the URLs parameter.
- Method Taskmethod = Clazz.getmethod ("Dotask", String. Class, String. class); //Then we can do something with reflection.
- Taskmethod.invoke (Clazz.newinstance (),"Hello","World");
Because the ClassLoader load class uses the overall responsibility for the delegation mechanism. The so-called overall responsibility, that is, when a classloader loading a class, the class depends on the and reference of all classes are also loaded by this classloader, unless explicitly using another classloader loading.
So, when our custom ClassLoader load succeeds Com.company.MyClass, all the dependent classes in MyClass are loaded by this classloader.
Custom ClassLoader can be useful in some scenarios, especially when you need the flexibility to dynamically load class.
The following article lists the application scenarios of one of the custom ClassLoader, and interested students can refer to the following:
http://longdick.iteye.com/blog/332580
Diagram ClassLoader The process of loading class and customizing ClassLoader