Order of Tomcat Classloaders: common, shared, and server

前端 未结 3 679
南笙
南笙 2021-02-07 08:33

The Tomcat Class Loader HOW-TO documentation describes 4 different class loaders:

  1. Bootstrap
  2. System
  3. Common
  4. Webapp

In the d

3条回答
  •  灰色年华
    2021-02-07 09:05

    I recently ran into this issue as well and here is what I found, (This is all from Tomcat 7 trunk)

    If left as blank, the "common" loader will be used as Catalina's "shared"/"server" loader.

    Here is the relevant source,

    89      private void initClassLoaders() {
    90          try {
    91              commonLoader = createClassLoader("common", null);
    92              if( commonLoader == null ) {
    93                  // no config file, default to this loader - we might be in a 'single' env.
    94                  commonLoader=this.getClass().getClassLoader();
    95              }
    96              catalinaLoader = createClassLoader("server", commonLoader);
    97              sharedLoader = createClassLoader("shared", commonLoader);
    98          } catch (Throwable t) {
    99              handleThrowable(t);
    100             log.error("Class loader creation threw exception", t);
    101             System.exit(1);
    102         }
    103     }
    
    106     private ClassLoader createClassLoader(String name, ClassLoader parent)
    107         throws Exception {
    108 
    109         String value = CatalinaProperties.getProperty(name + ".loader");
    110         if ((value == null) || (value.equals("")))
    111             return parent;
    

    So if nothing is defined, they fallback to using the common.loader entries.


    As to the order that they are loaded in, here is the source for loading them in, from source

    229         Thread.currentThread().setContextClassLoader(catalinaLoader);
    230 
    231         SecurityClassLoad.securityClassLoad(catalinaLoader);
    232 
    233         // Load our startup class and call its process() method
    234         if (log.isDebugEnabled())
    235             log.debug("Loading startup class");
    236         Class startupClass =
    237             catalinaLoader.loadClass
    238             ("org.apache.catalina.startup.Catalina");
    239         Object startupInstance = startupClass.newInstance();
    240 
    241         // Set the shared extensions class loader
    242         if (log.isDebugEnabled())
    243             log.debug("Setting startup class properties");
    244         String methodName = "setParentClassLoader";
    245         Class paramTypes[] = new Class[1];
    246         paramTypes[0] = Class.forName("java.lang.ClassLoader");
    247         Object paramValues[] = new Object[1];
    248         paramValues[0] = sharedLoader;
    249         Method method =
    250             startupInstance.getClass().getMethod(methodName, paramTypes);
    251         method.invoke(startupInstance, paramValues);
    

    Line 229 sets the common.loader classLoader, then line 251 sets the shared.loader classloader is set as Catalinas parent class loader.

提交回复
热议问题