细说tomcat之类加载器

官网:http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
Java类加载与Tomcat类加载器层级关系对比

Java ClassLoader:
    Bootstrap ClassLoader(加载$JAVA_HOME/jre/lib/目录下核心类库:resources.jar,rt.jar,sunrsasign.jar,jsse.jar,jce.jar,charsets.jar,jfr.jar,以及jre/classes目录下的class)
       /|        |                                                            Tomcat ClassLoader:
    ExtClassLoader(加载$JAVA_HOME/jre/lib/ext/目下的所有jar) -------- Bootstrap(加载$JAVA_HOME/jre/lib/ext/目录下的所有jar)
       /|\                                                                 /|        |                                                                   |
    AppClassLoader(加载应用程序classpath目录下的所有jar和class文件)    System(加载$CATALINA_HOME/bin/bootstrap.jar,$CATALINA_BASE/bin/tomcat-juli.jar,$CATALINA_HOME/bin/commons-daemon.jar)
                                                                           /|                                                                            |
                                                                         Common(加载$CATALINA_BASE/lib和$CATALINA_HOME/lib下的class,资源和jar文件)
                                                                           /|                                                                            |
                                                                         WebAppClassLoader(加载WebApp/WEB-INF/classes,WebApp/WEB-INF/lib)

Java ClassLoader验证:

public class ClassLoaderTest {
    public static void main(String[] args) {
        ClassLoader appClassLoader = ClassLoaderTest.class.getClassLoader();
        ClassLoader extClassLoader = appClassLoader.getParent();
        ClassLoader bootstrapClassLoader = extClassLoader.getParent();
        System.out.println(appClassLoader);
        System.out.println(extClassLoader);
        System.out.println(bootstrapClassLoader);
    }
}
输出:[email protected][email protected]nul

Tomcat ClassLoader验证:

public class ClassLoaderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ClassLoader loader = this.getClass().getClassLoader();
        while(loader != null) {
            System.out.println(loader);
            loader = loader.getParent();
        }
        System.out.println(loader);
    }
}

输出:
ParallelWebappClassLoader
  context: test-web
  delegate: false
----------> Parent Classloader:
[email protected]

[email protected]
[email protected]
[email protected]
null
跟踪org.apache.catalina.loader.ParallelWebappClassLoader源码发现,org.apache.catalina.loader.WebappClassLoaderBase重写了toString()方法:

@Override
public String toString() {

    StringBuilder sb = new StringBuilder(this.getClass().getSimpleName());
    sb.append("\r\n  context: ");
    sb.append(getContextName());
    sb.append("\r\n  delegate: ");
    sb.append(delegate);
    sb.append("\r\n");
    if (this.parent != null) {
        sb.append("----------> Parent Classloader:\r\n");
        sb.append(this.parent.toString());
        sb.append("\r\n");
    }
    if (this.transformers.size() > 0) {
        sb.append("----------> Class file transformers:\r\n");
        for (ClassFileTransformer transformer : this.transformers) {
            sb.append(transformer).append("\r\n");
        }
    }
    return (sb.toString());
}

也就是说,Tomcat的ClassLoader结构为:
null(JVM Bootstrap ClassLoader)
[email protected]
[email protected]
[email protected]
org.apache.catalina.loader.ParallelWebappClassLoader
Tomcat Common ClassLoader默认的搜索顺序为:
(1)unpacked classes and resources in $CATALINA_BASE/lib
(2)JAR files in $CATALINA_BASE/lib
(3)unpacked classes and resources in $CATALINA_BASE/lib
(4)JAR files in $CATALINA_HOME/lib
tomcat classloader类图

总结:
1. Java装载类使用“全盘负责委托机制”。
“全盘负责”是指当一个ClassLoder装载一个类时,除非显示地使用另外一个ClassLoder,否则该类所依赖及引用的类也由这个ClassLoder载入;
“委托机制”是指先委托父类装载器寻找目标类,只有在找不到的情况下才从自己的类路径中查找并装载目标类。
2. Tomcat的WebAppClassLoade默认不使用“委托机制”,查找class和资源的顺序如下:

(1)Bootstrap classes of your JVM
(2)/WEB-INF/classes of your web application
(3)/WEB-INF/lib/*.jar of your web application
(4)System class loader classes
(5)Common class loader classes
If the web application class loader is configured with <Loader delegate="true"/> then the order becomes:
(1)Bootstrap classes of your JVM
(2)System class loader classes
(3)Common class loader classes
(4)/WEB-INF/classes of your web application
(5)/WEB-INF/lib/*.jar of your web application

3. Tomcat中变量$CATALINA_BASE和$CATALINA_HOME的含义

Throughout the docs, you‘ll notice there are numerous references to $CATALINA_HOME的含义.
This represents the root of your Tomcat installation. When we say, "This information can be found in your $CATALINA_HOME/README.txt file" we mean to look at the README.txt file at the root of your Tomcat install.
Optionally, Tomcat may be configured for multiple instances by defining $CATALINA_BASE for each instance. If multiple instances are not configured, $CATALINA_BASE is the same as $CATALINA_HOME.

一言以蔽之;如果没有明确设置CATALINA_BASE变量,则CATALINA_BASE与CATALINA_BASE值相同,都是值tomcat安装目录。

【参考】
http://www.hollischuang.com/archives/199 深度分析Java的ClassLoader机制(源码级别)
http://blog.csdn.net/xyang81/article/details/7292380  深入分析Java ClassLoader原理

时间: 2024-08-09 02:09:00

细说tomcat之类加载器的相关文章

Tomcat内核之Tomcat的类加载器

跟其他主流的Java Web服务器一样,Tomcat也拥有不同的自定义类加载器,达到对各种资源库的控制.一般来说,Java Web服务器需要解决以下四个问题: ①   同一个Web服务器里,各个Web项目之间各自使用的Java类库要互相隔离. ②   同一个Web服务器里,各个Web项目之间可以提供共享的Java类库. ③   服务器为了不受Web项目的影响,应该使服务器的类库与应用程序的类库互相独立. ④   对于支持JSP的Web服务器,应该支持热插拔(hotswap)功能. 对于以上几个问

框架学习前基础加强 泛型,注解,反射(泛型&注解)应用案例,IOC,Servlet3.0,动态代理,类加载器

泛型 1. 泛型类 :具有一个或多个类型变量的类,称之为泛型类! class A<T> { } 2. 在创建泛型类实例时,需要为其类型变量赋值 A<String> a = new A<String>(); * 如果创建实例时,不给类型变量赋值,那么会有一个警告! 3. 泛型方法 :具有一个或多个类型变量的方法,称之为泛型方法! class A<T> { public T fun(T t1) {} } fun()方法不是泛型方法!它是泛型类中的一个方法! pu

深入拆解类加载器,这样的姿势你还不懂吗?

本文导读: 1.前奏,举个生活中的小栗子 2.为何Java类型加载.连接在程序运行期完成? 3.一个类在什么情况下才会被加载到JVM中? 什么是主动使用.被动使用?代码示例助你透彻理解类初始化的时机. 4.类的加载(Loading)内幕透彻剖析 类加载做的那些事儿.双亲委派模型工作过程.ClassLoader源码解析 5.Tomcat如何打破双亲委派模型的 6.上下文类加载器深入浅出剖析 7.最后总结 1.前奏,举个生活中的小栗子 春节马上要到了,大家是不是都在迫不及待的等着回家团圆了呢? 大春

Tomcat内核之类加载器工厂

Java虚拟机利用类加载器将类载入内存,以供使用.在此过程中类加载器要做很多的事情,例如读取字节数组.验证.解析.初始化等.而Java提供的URLClassLoader类能方便地将jar.class或网络资源加载到内存.Tomcat中则用一个工厂类ClassLoaderFactory把创建类加载器的细节进行封装,通过它可以很方便地创建自定义的类加载器. 如上图,利用createClassLoader方法并传入资源路径跟父类加载器即可创建一个自定义类加载器,此类加载器负责加载传入的所有资源. Cl

tomcat 7 中的类加载器学习

tomcat 7自带很多junit测试用例,可以帮助我们窥探源码的秘密.以下使用来测试类加载器的一个测试用例.类加载器也是对象,他们用来将类从类从.class文件加载到虚拟机,这些已经讲了很多,深入jvm中说的很详细,什么双亲委派模型,在书中还以tomcat为例讲解. /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the N

java类加载器-Tomcat类加载器

在上文中,已经介绍了系统类加载器以及类加载器的相关机制,还自定制类加载器的方式.接下来就以tomcat6为例看看tomat是如何使用自定制类加载器的.(本介绍是基于tomcat6.0.41,不同版本可能存在差异!) 网上所描述的tomcat类加载器 在网上搜一下“tomcat类加载器”会发现有大量的文章,在此我偷个懒,^_^把网上对tomcat类加载器的描述重说一下吧. CommonClassLoader:加载的类目录通过{tomcat}/conf/catalina.properties中的co

Tomcat类加载器

1JVM类加载机制 JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使用native代码实现,在rt.jar等包中搜索运行JVM所需的类,例如java.lang等包下的类. 2)扩展类加载器:负责载入标准扩展目录中的类,例如Sun的JVM的扩展目录是/jdk/jre/lib/ext. 3)系统类加载器:默认的类加载器,搜索环境变量CLASSPATH中指明的路径. 2双亲委

(转)《深入理解java虚拟机》学习笔记8——Tomcat类加载器体系结构

Tomcat 等主流Web服务器为了实现下面的基本功能,都实现了不止一个自定义的类加载器: (1).部署在同一个服务器上的两个web应用程序所使用的java类库可以相互隔离. (2).部署在同一个服务器上的两个web应用程序所使用的java类库可以相互共享. (3).许多Web服务器本身使用java语言实现,因此服务器所使用的类库应与应用程序的类库相互独立. (4).支持JSP应用的Web服务器,需要支持HotSwap功能,因为JSP文件最终是被编译为java的servlet来运行的,当修改JS

深入理解JVM虚拟机7:JNDI,OSGI,Tomcat类加载器实现

打破双亲委派模型 JNDI JNDI 的理解 JNDI是 Java 命名与文件夹接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之中的一个,不少专家觉得,没有透彻理解JNDI的意义和作用,就没有真正掌握J2EE特别是EJB的知识. 那么,JNDI究竟起什么作用?//带着问题看文章是最有效的 要了解JNDI的作用,我们能够从“假设不用JNDI我们如何做?用了JNDI后我们又将如何做?”这个问题来探讨. 没有JNDI的做法: 程序猿开发时,