English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
One, let's first look at the execution process of the code we write:
Two, Significance of Studying Class Loading Mechanism
As can be seen from the figure above, class loading is the first step in the execution of a Java program, studying class loading is helpful to understand the execution process of JVM, and guide developers to take more effective measures to cooperate with program execution.
The second purpose of studying the class loading mechanism is to allow the program to dynamically control class loading, such as hot deployment, etc., to improve the flexibility and adaptability of the program.
Three, General Process of Class Loading
Principle: Double-Parent Delegation Pattern
1、Search for the jre directory, search for jvm.dll, and initialize JVM;
2、Generate a Bootstrap Loader (Bootstrap Class Loader);
3、Bootstrap Loader automatically loads Extended Loader (Standard Extension Class Loader) and sets its parent Loader to Bootstrap Loader.
4、Bootstrap Loader automatically loads AppClass Loader (System Class Loader) and sets its parent Loader to Extended Loader.
5、Finally, the HelloWorld class is loaded by AppClass Loader.
Four, Characteristics of Class Loaders
1、When running a program, the AppClass Loader (System Class Loader) always starts loading the specified class.
2、When loading a class, each class loader will hand over the loading task to its parent, and if the parent cannot find it, load it by itself.
3、Bootstrap Loader (Bootstrap Class Loader) is the top-level class loader, whose parent loader is null.
Five, Getting the Class Loader
It's very simple, see the example below
public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); Class c = hello.getClass(); ClassLoader loader = c.getClassLoader(); System.out.println(loader); System.out.println(loader.getParent()); System.out.println(loader.getParent().getParent()); } }
Print result:
sun.misc.Launcher$AppClassLoader@19821f
sun.misc.Launcher$ExtClassLoader@addbf1
null
From the above results, it can be seen that the parent Loader of ExtClassLoader was not obtained, because Bootstrap Loader (the startup class loader) is implemented in C language, and there is no way to find a definite parent Loader, so it returns null.
Sixth, class loading
There are three ways to load classes:
1By JVM initialization loading when the application is started from the command line
2By dynamically loading through the Class.forName() method
3By dynamically loading through the ClassLoader.loadClass() method
The three methods have quite different characteristics, let's take an example to understand:
package zhongqiu.common.base; public class ClassLoadDemo { static { System.out.println("ClassLoadDemo static initialization block was executed!"); } public static void main(String[] args) throws ClassNotFoundException { ClassLoader loader2 = ClassLoadDemo.class.getClassLoader(); System.out.println(loader2); // Using ClassLoader.loadClass() to load a class, it will not execute the initialization block // loader2.loadClass("zhongqiu.test.Test"); // Using Class.forName() to load a class, it will execute the initialization block by default // Class.forName("zhongqiu.test.Test"); // Using Class.forName() to load a class, and specify ClassLoader, does not execute the static block during initialization Class.forName("zhongqiu.test.Test", false, loader2); } }
Custom ClassLoader
package zhongqiu.common.base.classload; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; public class MyClassLoader { @SuppressWarnings("resource") public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException { URL url = new URL("file:/D:/javaworkspace/JavaCommon/src/"); ClassLoader myloader = new URLClassLoader(new URL[] { url }); Class c = myloader.loadClass("zhongqiu.common.base.classload.Test"); Test t3 = (Test) c.newInstance(); } }
inJava.The 'lang' package contains a ClassLoader class. The basic goal of ClassLoader is to provide services for class requests, dynamically loading classes and resources as needed. A class loader will only load a class and initialize it when a class needs to be used ( instantiated using the 'new' keyword). A Java application can use different types of class loaders. For example, in a Web Application Server, the loading of Servlets uses a class loader custom-defined by the developer. The 'java.lang.String' is loaded using the JVM system class loader, the Bootstrap Class Loader, and other classes defined by the developer are loaded by the AppClassLoader. In the JVM, different Java types are distinguished by class name and class loader. Therefore, the JVM allows us to use different loaders to load Java classes with the same namespace, and in fact, these Java classes with the same namespace can be completely different classes. This mechanism ensures that the 'java.lang.String' provided by JDK is unique.
8. Why should we use this parent delegation model?
This can avoid repeated loading, when the father has loaded the class, there is no need for the child ClassLoader to load it again.
Considering the safety factors, let's imagine that if we do not use this delegation model, we can use a custom String to dynamically replace the defined types in the java core API at any time, which will exist a very large security risk, and the delegation mode of the parent can avoid this situation, because String has been loaded at startup, so the user-defined class cannot load a custom ClassLoader.
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Naya Tutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)