注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

断尘居

温柔的男人像海洋。

 
 
 
 
 

日志

 
 

Tomcat类加载机制  

2010-10-24 23:10:07|  分类: Tomcat/JBoss |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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目录下。

  评论这张
 
阅读(1927)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017