http://blog.163.com/haizai219@126/blog/static/44412555200810111429791/
http://andyao.iteye.com/blog/34409
)
Tomcat Server在启动的时候将构造一个ClassLoader树,以保证模块的类库是私有的
Tomcat Server的ClassLoader结构如下:
|
System
|
Common
/ \
Catalina Shared
/ ... ... \
webapp1 webappN
Bootstrap
|
System
|
Common
/ \
Catalina Shared
/ \
WebApp1 WebApp2
各个类加载器的作用描述如下:
1)Bootstrap ClassLoader
负责加载由虚拟机提供的基本运行时类和系统扩展目录($JAVA_HOME/jre/lib/ext)下的JAR包;
2)System ClassLoader
通 常这个加载器用来加载CLASSPATH环境变量中指定的类,但在Tomcat5的标准启动脚本($CATALINA_HOME/bin /catalina.sh或%CATALINA_HOME%/bin/catalina.bat)中改变了它的行为,它只加载下面的类:
$CATALINA_HOME/bin/bootstrap.jar // Contains the main() method that is used to initialize the Tomcat 5 server, and the class loader implementation classes it depends on.
$JAVA_HOME/lib/tools.jar // Contains the "javac" compiler used to convert JSP pages into servlet classes.
$CATALINA_HOME/bin/commons-logging-api.jar // Jakarta commons logging API.
$CATALINA_HOME/bin/commons-daemon.jar // Jakarta commons daemon API.
jmx.jar // The JMX 1.2 implementation.
3)Common ClassLoader
它负责加载Tomcat本身和所有的web应用都能看到的类。通常,应用的类不应该由他加载。 $CATALINA_HOME/common/classes,$CATALINA_HOME/commons/endorsed和$CATALINA_HOME/common/lib下的类都由这个加载器加载。缺省的,包括:
ant.jar - Apache Ant.
commons-collection.jar // Jakarta commons collection .
commons-dbcp.jar // Jakarta commons DBCP, providing a JDBC connection pool to web applications.
commons-el.jar // Jakarta commons el, implementing the expression language used by Jasper.
commons-pool.jar // Jakarta commons pool .
jasper-compiler.jar // The JSP 2.0 compiler .
jasper-runtime.jar // The JSP 2.0 runtime.
jsp-api.jar // The JSP 2.0 API.
naming-common.jar // The JNDI implementation used by Tomcat 5 to represent in-memory naming contexts.
naming-factory.jar // The JNDI implementation used by Tomcat 5 to resolve references to enterprise resources (EJB, connection pools).
naming-resources.jar // The specialized JNDI naming context implementation used to represent the static resources of a web application.
servlet-api.jar // The Servlet and JSP API classes.
xerces.jar // The XML parser that is visible by default to Tomcat internal classes and to web applications.
xml-apis.jar
4)Catalina ClassLoader
用来加载实现Tomcat自己需要的类。由它加载的类对web应用都是不可见的。$CATALINA_HOME/server/classes,$CATALINA_HOME/server/lib,都由这个加载器加载。缺省的,包括:
catalina.jar //Implementation of the Catalina servlet container portion of Tomcat 5.
jakarta-regexp-X.Y.jar //The binary distribution of the Jakarta Regexp regular expression processing library, used in the implementation of request filters.
servlets-xxxxx.jar //The classes associated with each internal servlet that provides part of Tomcat's functionality. These are separated so that they can be completely removed if the corresponding service is not required, or they can be subject to specialized security manager permissions.
tomcat-coyote.jar - Coyote connector for Tomcat 5.
tomcat-http11.jar //Standalone Java HTTP/1.1 connector.
tomcat-jk2.jar //Classes for the Java portion of the JK 2 web server connector, which allows Tomcat to run behind web servers such as Apache and iPlanet iAS and iWS.
tomcat-util.jar //Utility classes required by some Tomcat connectors.
5)Shared ClassLoader
被所有的web应用共享的类和资源由这个加载器加载。$CATALINA_HOME/shared/classed,$CATALINA_HOME/shared/lib,都由这个加载器加载。装载的类们仅对所有WEB APP可见,对TOMCAT不可见(也不必见)
6) W ebappX ClassLoader
对每个Tomcat里的web应用都创建一个加载器,web应用下的WEB-INF/classes,WEB-INF/lib,都由这个加载器加载,由它所加载的类对其他的web应用是不可见的。
web 应用的加载器(WebappX)和JDK的委托模型略有不同,这是根据Servlet2.3规范做出的。当WebappX被请求加载一个类时,它首先尝试 自己加载,而不是先委托给它的父加载器加载,如果无法加载该类,则委托它的父加载器加载。但是,对于下面的类,仍然要先委托给父加载器:
Classes which are part of the JRE base classes cannot be overriden. For some classes (such as the XML parser components in JDK 1.4+), the JDK 1.4 endorsed feature can be used (see the common classloader definition above). In addition, for the following class patterns, the classloader will always delegate first (and load the class itself if no parent classloader loads it):
javax.*
org.xml.sax.*
org.w3c.dom.*
org.apache.xerces.*
org.apache.xalan.*
Last, any JAR containing servlet API classes will be ignored by the classloader.
Tomcat中其他的加载器都是遵循委托模型的。
最后,以web应用的角度,要加载类或者资源时,会以下面的顺序查找:
Bootstrap classes of your JVM
System class loader classses (described above)
/WEB-INF/classes of your web application
/WEB-INF/lib/*.jar of your web application
$CATALINA_HOME/common/classes
$CATALINA_HOME/common/endorsed/*.jar
$CATALINA_HOME/common/lib/*.jar
$CATALINA_BASE/shared/classes
$CATALINA_BASE/shared/lib/*.jar
简要概括:
- Bootstrap - 载入JVM自带的类和$JAVA_HOME/jre/lib/ext/*.jar
- System - 载入$CLASSPATH/*.class
- Common - 载入$CATALINA_HOME/common/...,它们对TOMCAT和所有的WEB APP都可见
- Catalina - 载入$CATALINA_HOME/server/...,它们仅对TOMCAT可见,对所有的WEB APP都不可见
- Shared - 载入$CATALINA_HOME/shared/...,它们仅对所有WEB APP可见,对TOMCAT不可见(也不必见)
- WebApp - 载入ContextBase?/WEB-INF/...,它们仅对该WEB APP可见
每个运行中的线程都有一个成员contextClassLoader,用来在运行时动态地载入其它类,系统默认的 contextClassLoader是systemClassLoader,所以一般而言java程序在执行时可以使用JVM自带的 类、$JAVA_HOME/jre/lib/ext/中的类和$CLASSPATH/中的类,可以使用下述方法更改当前线程的 contextClassLoader,来改变其载入类的行为。
Thread.currentThread().setContextClassLoader(...);
ClassLoader被组织成树形,一般的工作原理是:
1) 线程需要用到某个类,于是contextClassLoader被请求来载入该类
2) contextClassLoader请求它的父ClassLoader来完成该载入请求
3) 如果父ClassLoader无法载入类,则contextClassLoader试图自己来载入
注意:WebApp?ClassLoader的工作原理和上述有少许不同:
它先试图自己载入类(在ContextBase?/WEB-INF/...中载入类),如果无法载入,再请求父ClassLoader完成
由此可得:
- 对于WEB APP线程,它的contextClassLoader是WebApp?ClassLoader
- 对于Tomcat Server线程,它的contextClassLoader是CatalinaClassLoader
结论
对于只用于某一个web应用的类或资源 ,放在这个web应用下的/WEB-INF/classes目录下,如果是JAR,就放在这个web应用下的WEB-INF/lib目录下。
对于让所有的web应用共享的类或资源 ,放在$CATALINA_HOME/shared/classes目录下,如果是JAR,就放在$CATALINA_HOME/shared/lib目录下。