Tomcat类加载器的顺序:common,shared和server [英] Order of Tomcat Classloaders: common, shared, and server

查看:2395
本文介绍了Tomcat类加载器的顺序:common,shared和server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tomcat 类加载器HOW-TO 文档描述了4个不同的类加载器:

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


  1. Bootstrap

  2. 系统

  3. li>
  4. 常见

然而,在默认的catalina.properties文件中,和服务器类加载器。在文件的默认版本中,这两个属性都是空的,注释说:

In the default catalina.properties file, however, there are properties defined for a shared and server class loader as well. In the default version of the file both of these properties are empty and the comments say:


如果留空,
将用作Catalina的
共享/服务器加载程序。

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

查找关于这些类加载器的任何其他文档。我的问题是,共享和系统加载器相对于共同加载器搜索的顺序是什么?此外,这些类加载器的预期用途是什么?

I have not been able to find any additional documentation about these class loaders. My question is, in what order are the shared and system loaders searched relative to the common loader? And additionally, what is the intended use for these class loaders?

推荐答案

我最近遇到了这个问题,我发现,(这都是从Tomcat 7树干)

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

首先,如果留空,common加载器将被用作Catalina 共享/服务器加载程序。

这里是相关的 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;

所以如果没有定义,他们会使用common.loader条目。

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);

第229行设置common.loader类加载器,然后第251行设置shared.loader类加载器设置为Catalinas父类加载器。

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

这篇关于Tomcat类加载器的顺序:common,shared和server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆