Tomcat类加载机制

 


Tomcat类加载机制
各个类加载器的作用描述如下:
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应用都能看到的类。通常,应用的类不应该由他加载。

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,都由这个加载器加载。缺省的,包括:

jakarta-regexp-X.Y.jar - The binary distribution of the Jakarta Regexp regular expression processing library, used in the implementation of request filters.

tomcat-coyote.jar - Coyote connector for Tomcat 5.

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.

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被请求加载一个类时,它首先尝试自己加载,而不是先委托给它的父加载器加载,如果无法加载该类,则委托它的父加载器加载。但是,对于下面的类,仍然要先委托给父加载器:

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.

最后,以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目录下。

 

http://idealab.iteye.com/blog/361243

类加载器原理: http://www.importnew.com/6581.html

 

你可能感兴趣的:(tomcat)