What is the principle behind the implementation of the Spring class loader?
Spring utilizes the parent delegation model to implement class loaders. In Spring, the two main class loaders used are AppClassLoader and ExtClassLoader.
AppClassLoader is the application class loader, a subclass of the ClassLoader class, responsible for loading classes in an application. When a class needs to be loaded, AppClassLoader first checks if it has already loaded the class. If not, it delegates to ExtClassLoader to try and load it. If ExtClassLoader also has not loaded the class, then AppClassLoader will try to find and load the class from the classpath.
ExtClassLoader is the extension class loader, which is a subclass of the ClassLoader class, responsible for loading Java’s extension libraries. When a class needs to be loaded, ExtClassLoader first checks if it has already loaded the class. If not, it delegates to the BootstrapClassLoader to try to load it. If BootstrapClassLoader has not loaded the class either, then ExtClassLoader will try to locate and load the class from the JRE’s extension directory.
BootstrapClassLoader is the boot class loader, built into the JVM, responsible for loading the classes needed by the JVM itself. It serves as the root loader for all class loaders, without a parent loader. When a class needs to be loaded, BootstrapClassLoader will first attempt to locate and load the class from the JVM’s internal class library.
This is the basic principle of how Spring class loaders work. By using the delegation model, Spring ensures consistency in the order and process of loading classes, preventing duplicate loading and conflicts. Additionally, Spring offers custom class loaders for loading specific classes or resources to meet different needs.