Launcher Startup Class
This article is the source analysis part of the parent delegation mechanism, and the parent delegation model in the class loading mechanism is very important for the stable operation of the JVM. But the source is actually relatively simple, then briefly introduce let's start with the startup class. there is a Launcher class Sun.misc.Launcher; take a closer look at this short line of comments to get useful information PS: Directly inside the IDE to view the anti-compilation, do not see the comments, you can download openjdk view the source code, my version is openjdk-8-src-b132-03_mar_2014
Sun.misc.Launcher This class is the initiator that the system uses to start the main application |
construction Method Launcher () has done four things
Creating an Extension class loader |
To create an application class loader |
Set Contextclassloader |
If you need to install Safety Manager Security Manager
|
where launcher is STAITC, so the initialization will create the object, that is, trigger the construction method, so the initialization time will be executed the above four steps Extclassloader and Appclassloader are both static inner classes of launcher. and they're all ClassLoader implementations. Take a look at the key steps in the creation of Extclassloader also looking at the key steps in the creation of Appclassloader In addition, there are static variables in the Launcher class you should be able to imagine what these three are, and if you don't, you need to study it again . system.getproperty ("Sun.boot.class.path") system.getproperty ("Java.ext.dirs") system.getproperty ("Java.class.path") ClassLoader's
Construction Method
as I said earlier, there are only two kinds of loaders for virtual machines start the ClassLoader and everything else, and all the others are Java.lang.ClassLoader subclasses so if you want to customize the ClassLoader, you must inherit the implementation ClassLoader And , as we said above, the Appclassloader and Extclassloader provided by Java are all ClassLoader subclasses. look at the
construction methods and variables of the ClassLoader
parent you will find that the construction method actually has only two-parameter versions of this kind The second parameter is parent, which is a classloader that records his parent ClassLoader
regardless of which construction method is called The parent is bound to be initialized either you call a constructor with parameters and explicitly specify one to set the parent If you do not specify, the default constructor method will use the Appclassloader returned by Getsystemclassloader to set the parent |
PS: In many places in this article, I've added a few spaces between the parent and class loader in the parent ClassLoader do not understand it as a parent ClassLoader,< parent ClassLoader > refers to the class loader's load order hierarchy precedence rather than the parent class in the inheritance relationship that is usually said to mean his previous level
Getsystemclassloader getting the Appclassloader process |
then look back. Construction of the Application class loader The extension ClassLoader was passed to him as a parameter, and he finally called the constructor of a parameter of ClassLoader.
set Extclassloader to Appclassloader's parent and Extclassloader, his parent is null PS: Startup ClassLoader is part of the virtual machine and may be c/c++/java implemented, so it is not part of the Java language So for Java itself, it can be said that he does not exist, but the JVM is aware of his So, here is null, and parent is null to indicate that his parent ClassLoader is the Launcher class loader or maybe it is the bootloader itself. LoadClass and Findclass To
implement the ClassLoader, you need to inherit ClassLoader
and there are two important ways. look at the statement of two important methods, you may feel it, think about public and protected what is the meaning?
The LoadClass method is a method that the ClassLoader executes to load class logic, including checking whether it has been loaded, calling the parent class to load, and failing to use the Findclass method to load |
Findclass the actual behavior of the current ClassLoader for loading binary streams
|
The LoadClass method in Launcher.appclassloader, which is ultimately called Super.loadclass, is actually the LoadClass method of ClassLoader Launcher.extclassloader simply did not implement their own LoadClass method, so the use of ClassLoader in the |
and look at the LoadClass method of ClassLoader. He will call the parent's LoadClass method, and if his parent is not empty, it will be called until the top-most startup class loader if the Startup class loader is still not found, then call itself Findclass What if you call Findclass load failed ? Obviously, after the function call is finished, it is returned to the call point location, the form of the call stack . That is, after Must continue to carry out his next paragraph. If you don't throw an exception, you're going to come down here. apparently this completes a whole parent-delegated class loading mode Summarize Launcher as Initiator created the Extclassloader and Appclassloader They are all classloader subclasses, and ClassLoader has a parent that points to his class loader It is this property that completes the top-down prioritization of the order of precedence levels for Sun built-in Extclassloader as well as Appclassloader and launch class loaders Bootstrap their hierarchy Bootstrap>extclassloader>extclassloader and they each have a different division of labor through the LoadClass method of ClassLoader, their invocation logic is determined, that is, the parent delegation mechanism each level will pass the class load request upward, and only the upper parent ClassLoader call fails to attempt to load Parental delegation mechanism is of great significance, resulting in higher security and other advantages But the logic of his implementation is really simple. a loadclass will take care of it. Findclass is the specific behavior of the class loader's own load class So, if you don't need to destroy the parent delegation mechanism, you just need to override this method to if you want to fully customize the logic mechanism of your classloader, directly overwrite LoadClass, of course, you may also need to continue overwriting findclass