Tomcat class loader

zhaozj2021-02-16  163

1 - Tomcat class loader structure

Tomcat Server will construct a ClassLoader tree when started, to ensure that the module class library is the private Tomcat Server's ClassLoader structure as follows:

-----------------------------

| Bootstrap |

| | | | |

| System |

| | | | |

Common |

| / / |

| Catalina Shared |

| / / |

| WebApp1 WebApp2 |

-----------------------------

Among them: - Bootstrap - Loaded into the JVM comes with $ java_home / jre / lib / ext / *. Jar- system - load $ ClassPath / *. Class- Common - load $ Catalina_Home / Common / ..., They are visible to Tomcat and all web apps - Catalina - load $ Catalina_Home / Server / ..., they are only visible to Tomcat, are not visible to all Web App - Shared - load $ catalina_home / share / .. They are only visible to all Web App, which is not visible to Tomcat (no need to see) - WebApp? - Load CONTEXTBASE? / Web-inf / ..., they are only visible to the web app

2 - ClassLoader working principle

Each running thread has a member ContextClassLoader, which is used to dynamically load other system default ContextClassLoader when running is SystemClassLoader, so the Java program can use the JVM comseound when executed, $ java_home / The classes in JRE / lib / ext / and class in the $ ClassPath can use thread.currentthread (). setContextClassLoader (...); change the CONTEXTCLASSLOADER of the current thread to change its loaded behavior

ClassLoader is organized into a tree, the general working principle is: 1) Threads need to use a class, so ContextClassLoader is requested to load this class 2) ContextClassLoader Request Its Father ClassLoader to complete the load request 3) if the father ClassLoader cannot load classes, then ContextClassLoader tries to load itself.

Note: WebApp? ClassLoader works and a little different: it tries yourself to load classes (in ContextBase? / Web-inf / ...), if you can't load, please ask the father ClassLoader to complete

This is available: - For the web app thread, its contextClassLoader is WebApp? ClassLoader - For Tomcat Server thread, its conteclassloader is CatalinAclassLoader

3 - Some original code analysis

3.1 - ORG / APACHE / CATALINA / Startup / Bootstrap.java

The starting point of the Tomcat Server thread constructs the ClassLoader tree and sets the Tomcat Server thread to load several classes for Catalinaloader, then transfer to org.apache.catalina.startup.catalina class [View Code]

3.2 - ORG / APACHE / CATALINA / Startup / ClassLoaderFactory.JAVA

Create and return instances of StandardClassLoad according to settings

[View Code]

3.3 - ORG / APACHE / CATALINA / LOADER / STANDARDCLASSLOADER.JAVA

Class loader

3.4 - ORG / APACHE / CATALINA / Startup / SecurityClassLoad.java

This class contains only a static method to load some classes for CatalinaDER.

[View Code]

Appendix - Reference

[1] http://jakarta.apache.org/tomcat/ of Tomcat 4.1.x Document Class Loader How-To

转载请注明原文地址:https://www.9cbs.com/read-8525.html

New Post(0)