The Java classloader involves three classes: bootstrapClassLoader; Extclassloader; Appclassloader.
The parent delegate mechanism for Java class loading: When a file needs to be loaded, the AppClassloader checks its cache to see if the class has been loaded, and returns it if it has. If the class has not been loaded, it delegates up to its parent, extClassLoader. The extClassLoader checks its cache to see if the class has been loaded. If it has been loaded, it returns directly. The bootstrapClassloader checks its cache to see if the class has been loaded, and returns if it has. If not, it checks to see if the class is included in its load path, and if so, it loads the class and returns the result. If it does not, it posts down to its child loader, extClassLoader, which checks its load directory to see if it contains the class. If it does, it loads the class and returns the result. If it does not, it continues to post to its child appClassLoader, which eventually returns to the appClassLoader at the application level, which loads the class.
From the process of class loading, we can see that: 1. Each class loader has its own cache, and the loaded classes are cached to avoid repeated loading, achieve reuse effect and improve loading efficiency; 2. 2. This practice reinforces security and prevents us from fooling the JVM with custom system classes.
The three classloaders of the classload, although there is a parent loader, child loader relationship but they do not have an inheritance relationship. Classloader is an abstract class that defines the basic methods of class loading. Secureclassloader extends the classLoader class and adds some permissions to enhance security. Urlclassloader inherits SecureClassLoader to load classes and resources through THE URL path. Extclassloader and AppClassLoader both inherit child URlClassLoader, and they are responsible for different loading paths.
Bootstrapclassloader does not inherit from classLoader. It is a class written in C/C++ and cannot be referenced by the Java layer, so null is returned when we use extClassLoader.getparent (). But they do have a parent loader and child loader relationship directly. The BootstrapClassLoader is responsible for loading classes that the JVM virtual machine needs to run, and it starts the JVM by creating an initial class.
To implement a classloader on your own, you need 1. Inherit the abstract classloader, 2. Overwrite the findClass () method and call defineclass () in it.