Java class loading takes a full-fledged delegation mechanism, and the subclass will delegate to the parent class when the class is loaded by default, but the subclass will attempt to load the class itself when the parent class is not loaded. This mechanism can effectively prevent a class from being loaded multiple times, and also to some extent prevent rewriting the JDK's own class "Java itself to restrict the class of Java at the beginning of the package can only be loaded by the root loader." However, this mode does not resolve the following issues:
1, the Java SPI application scenario, the interface core code is loaded by the root loader, but the interface implementation class is implemented by a third party, the root loader cannot load under Classpath, and the root loader cannot delegate its descendant class loader to load the class "It also does not know the existence of its descendants class loader?" "And how to solve this problem?" Using the thread context loader, in the SPI Interface code portion of the root loader load, by getting the thread context class loader instance "default is the system ClassLoader", use it to load the concrete implementation class, and then get to the class object.
2, the Web container class loading requirements, general Web container class loading to achieve the application of different classes of isolation, but also to enable the sharing of the common library of the application, to achieve the Web application and the container itself of the class library isolation and sharing. Therefore, it cannot be implemented entirely using the parent-class delegate-loading mechanism. The general practice is that the class loader of each application first tries to load the class in its own directory, and does not load to request that the parent class load the class "application's own class library takes precedence over the common class library being loaded." Tomcat's own class loading mechanism also follows the idea that, by default, the class library under the Web application's own directory has an application ClassLoader loaded, and the class library under the common shared directory has the common ClassLoader loaded.
Java class loading mechanism
Java class Loader ClassLoader summary
In layman ClassLoader
Java ClassLoader Review