Class Loader
function: Get binary byte stream by class fully qualified name
Use cases: Class-level partitioning, OSGi, hot deployment, code encryption
1, Class and class loader
Any class and the loader that loads the class together establish uniqueness in the virtual machine;
Each class has a separate class name space;
Determine whether two classes are equal and must be loaded with the same classloader; otherwise, it will affect: equals, IsAssignableFrom, isinstance, instanceof results
Packagecom.classload.temp;Importjava.io.IOException;ImportJava.io.InputStream; Public classMaintest { Public Static voidMain (string[] args)throwsclassnotfoundexception, Instantiationexception, illegalaccessexception {ClassLoader Myloader=NewClassLoader () {@Override PublicClass<?> loadclass (String name)throwsClassNotFoundException {Try{String FileName= Name.substring (Name.lastindexof (".") + 1) + ". Class"; InputStream is=getclass (). getResourceAsStream (FileName); if(IS = =NULL){ return Super. LoadClass (name); } byte[] B =New byte[Is.available ()]; Is.read (b); returnDefineClass (name, B, 0, b.length); } Catch(IOException e) {Throw Newclassnotfoundexception (name); } } }; Object obj= Myloader.loadclass ("Com.classload.temp.MainTest"). newinstance (); System.out.println (Obj.getclass ()); System.out.println (objinstanceofcom.classload.temp.MainTest); /*** Execution Result: * Class Com.classload.temp.MainTest * false/ /obj uses the Myloader custom class loader, Maintest uses the application ClassLoader, and the loader does not have to be false*/ }}
2, parental delegation model
Class loader classification
Mode one: ①, starting the ClassLoader: C + + implementation, which is part of the virtual machine itself
②, other ClassLoader: Java implementation, independent of the outside of the virtual machine, all inherit from the abstract class Java.lang.ClassLoader
Mode two: ①, starting the ClassLoader: loading the Java_home/lib directory, or being-xbootclasspath under the specified path, and being loaded into virtual machine memory by the class library recognized by the virtual machine
Boot loader cannot be referenced directly by user
The user writes the custom ClassLoader and needs to delegate the load request to the Boot class loader, and the custom class loader uses NULL instead.
②, extension ClassLoader: Class library under load Java_home/lib/ext directory, or path specified by java.ext.dirs system variable
Extension class loader can be used directly by the user
Sun.misc.launcher$extclassloader implementation
③, application ClassLoader: Load the class library specified on the user's classpath
The ClassLoader is the return value of the Getsystemclassloader () method in the ClassLoader, known as the System class loader
This type of loader can be used directly by the user
Sun.misc.launcher$appclassloader implementation
The relationship between class loaders uses a composite relationship to reuse the parent ClassLoader code instead of inheriting;
Parent delegation Model Work process:
If a class loader receives a request from the ClassLoader, it does not attempt to load itself, but instead delegates the request to the parent ClassLoader, which is the same for each level of the ClassLoader;
Therefore all class load requests are eventually routed to the top-level classloader, and the child loader will not load until the parent ClassLoader fails to load (the class is not found in the search scope).
Parental delegation Model Benefits:
The ①,java class has a hierarchical relationship with precedence as its classloader
such as: Java.lang.Object stored in Rt.jar no matter which class loader loads the class, it will be delegated to the top-level startup ClassLoader to load,
So the object class is the same class in the various class loaders of the program
such as: Writing a Java class with the same name as the Rt.jar class library, can be compiled normally but will never be loaded run (because parent delegation)
②, parental delegation Model is not a mandatory constraint model
Parent delegation Principle:
1, check if the class has been loaded
2, not loaded:
Parent ClassLoader non-null: Call the LoadClass () method of the parent class loader,
The parent loader is empty: default to use the startup ClassLoader as the parent class loader
The parent ClassLoader failed to load: Call its own Findclass () method to load
3, destroying the parental assignment model
java-class loading mechanism (3)-Class loader