(转)Tomcat类加载机制

http://blog.163.com/haizai219@126/blog/static/44412555200810111429791/

Tomcat类加载机制  

2008-11-11 13:42:09|  分类: Tomcat |  标签:tomcat  |字号 订阅

 
 

Tomcat中的类加载策略和JDK中的委托模型略有不同。Tomcat Server在启动的时候将构造一个ClassLoader树,以保证模块的类库是私有的,ClassLoader结构如下:
             Bootstrap
                   |
              System
                  |
            Common
          /                \
 Catalina          Shared
                      /  ... ...    \
            webapp1        webappN 

各个类加载器的作用描述如下:
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.

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

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.
Shared ClassLoader:被所有的web应用共享的类和资源由这个加载器加载。$CATALINA_BASE/shared/classed,$CATALINA_BASE/shared/lib,都由这个加载器加载。
WebappX 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

结论

对于只用于某一个web应用的类或资源,放在这个web应用下的/WEB-INF/classes目录下,如果是JAR,就放在这个web应用下的WEB-INF/lib目录下。
     对于让所有的web应用共享的类或资源,放在$CATALINA_BASE/shared/classes目录下,如果是JAR,就放在$CATALINA_BASE/shared/lib目录下。

 

 
 
 

猜你喜欢

转载自kavy.iteye.com/blog/1982818