To understand ClassLoader, we can explain by what and how two aspects
A: What is ClassLoader?
1. ClassLoader can be a class file loaded into the JVM method area.
2, ClassLoader is mainly divided into 2 categories, user-defined class loaders and internal class loaders: start the inside loader (bootstrap) and the user-defined loader (user-defined class loader).
The Startup class loader is divided into 3 classes:Bootstrap ClassLoader: Start ClassLoader, the topmost class loader in the Java class load hierarchy, is the parent loader for Extension ClassLoader, because Bootstrap ClassLoader is written in C + + and is not found in Java code
the class. Mainly responsible for loading the core libraries in the JDK, such as: Rt.jar, Resources.jar, Charsets.jar, etc.
Extension ClassLoader: called the Extension class loader, is responsible for loading the Java Extension Class library, loading all the jars under java_home/jre/lib/ext/by default.
Appclassloader: Called the system ClassLoader, is responsible for loading all the jar and class files in the application Classpath directory.
3, the ClassLoader load principle:
ClassLoader Load Class Principle: the use of parental delegation mechanism; When classloader needs to load a class, it will hand the search task to the parent loader to try to load before searching for the class. So the search process is from Bootstrap classloader-->
Extension classloader-->extension Classloader-->app ClassLoader in turn, finds the corresponding class and loads the object into the JVM. This delegation mechanism is used primarily to prevent people from maliciously modifying the core code of the JDK, such as creating your own
A class is a string, the package name is also Java.lang, and if there is no delegation mechanism, the string class that you define may be loaded when you call the string classes. Instead of the string class provided by the JDK. The two-delegate mechanism is searched from Bootstrap ClassLoader, in the core library of Java
When the class is found, the class is loaded directly into the JVM, and the string class you write is not read.
1 // gets the current class loader 2 ClassLoader loader=Thread.CurrentThread (). Getcontextclassloader (); 3 System.out.println (loader); 4 while (loader!=null) {5 loader=loader.getparent (); 6 System.out.println (loader); 7 }
The output is:
How to: Customize the class loader
1, Application scenario: We need to load the online class file into memory, using this class to implement the business logic required for our project.
2. The custom ClassLoader is divided into two steps: Inheriting the ClassLoader class, overriding the Findclass method
Packageedu.test;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.net.URL; Public classNetworkclassloaderextendsClassLoader {PrivateString Rooturl; PublicNetworkclassloader (String rooturl) { This. Rooturl =Rooturl; } @OverrideprotectedClass<?> Findclass (String name)throwsclassnotfoundexception {Class clazz=NULL; byte[] Classdata = getclassdate (name);//gets the byte-code array of the class file, based on the binary name of the if(Classdata = =NULL) { Throw Newclassnotfoundexception (); } clazz= DefineClass (name, Classdata, 0, classdata.length);//converts a class's byte-code array to an instance of class returnClazz; } //gets the byte-code file for the class file, based on the binary name of the Private byte[] getclassdate (String name) {InputStream input=NULL; Bytearrayoutputstream BAOs=NULL; String Path=Classnametopath (name); Try{URL URL=NewURL (path); byte[] Buff =New byte[1024 * 4]; intLen =-1; Input=Url.openstream (); BAOs=NewBytearrayoutputstream (); while(len = input.read (buff))! =-1) {baos.write (buff,0, Len); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Input = =NULL) { Try{input.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnBaos.tobytearray (); } Privatestring Classnametopath (string name) {returnRooturl + "/" + Name.replace (".", "/") + ". Class"; }}
Class loader Test
Public classClassloadertest { Public Static voidMain (string[] args) {Try{String Rooturl= "Http://localhost:8080/httpweb/classes"; Networkclassloader Networkclassloader=NewNetworkclassloader (Rooturl); String ClassName= "Org.classloader.simple.NetClassLoaderTest"; Class Clazz=Networkclassloader.loadclass (classname); System.out.println (Clazz.getclassloader ()); } Catch (Exception e) {e.printstacktrace (); } } }
ClassLoader Brief Introduction