Tomcat类加载机制
Tomcat类加载机制
转载来源: 中国IT实验室作者: 未知责编: 宝良
Tomcat Server在启动的时候将构造一个ClassLoader树,以保证模块的类库是私有的Tomcat Server的ClassLoader结构如下:
代码
Bootstrap
|
System
|
Common
/
Catalina Shared
/
WebApp1 WebApp2
其中:
- Bootstrap - 载入JVM自带的类和$JA V A_HOME/jre/lib/ext/*.jar
- System - 载入$CLASSPA TH/*.class
- Common - 载入$CA TALINA_HOME/common/...,它们对TOMCA T和所有的WEB APP都可见
- Catalina - 载入$CA TALINA_HOME/server/...,它们仅对TOMCAT可见,对所有的WEB APP 都不可见
- Shared - 载入$CA TALINA_HOME/shared/...,它们仅对所有WEB APP可见,对TOMCA T 不可见(也不必见)
- WebApp - 载入ContextBase?/WEB-INF/...,它们仅对该WEB APP可见
每个运行中的线程都有一个成员contextClassLoader,用来在运行时动态地载入其它类,系统默认的contextClassLoader是systemClassLoader,所以一般而言java程序在执行时可以使用JVM自带的类、$ JA V A_HOME/jre/lib/ext/中的类和$CLASSPATH/中的类,可以使用Thread.currentThread(). setContextClassLoader(...);更改当前线程的contextClassLoader,来改变其载入类的行为
ClassLoader被组织成树形,一般的工作原理是:
1) 线程需要用到某个类,于是contextClassLoader被请求来载入该类
2) contextClassLoader请求它的父ClassLoader来完成该载入请求
3) 如果父ClassLoader无法载入类,则contextClassLoader试图自己来载入
注意:WebApp?ClassLoader的工作原理和上述有少许不同:
它先试图自己载入类(在ContextBase?/WEB-INF/...中载入类),如果无法载入,再请求父ClassLoader完成
由此可得:
- 对于WEB APP线程,它的contextClassLoader是WebApp?ClassLoader
- 对于Tomcat Server线程,它的contextClassLoader是CatalinaClassLoader TOMCAT的类加载
【IT168技术文档】对于只用于某一个web应用的类或资源,放在这个web应用下的/WEB-INF/classes目录下,如果是JAR,就放在这个web应用下的WEB-INF/lib目录下。
对于让所有的web应用共享的类或资源,放在$CA TALINA_BASE/shared/classes目录下,如果是JAR,就放在$CA TALINA_BASE/shared/lib目录下。
Tomcat中的类加载策略和JDK中的委托模型略有不同。当Tomcat启动的时候,会创建一组类加载器,形成下面的层次关系:
Bootstrap
|
System
|
Common
/ \
Catalina Shared
/ \
webapp1 webapp2 ......
各个类加载器的作用描述如下:
Bootstrap:负责加载由虚拟机提供的基本的运行时类和系统扩展目录($JA V A_HOME/jre/lib/ext)下的JAR包;
System:通常这个加载器用来加载CLASSPA TH环境变量中指定的类,但在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.
$JA V A_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:它负责加载对于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.
Catalina:用来加载实现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:被所有的web应用共享的类和资源由这个加载器加载。$CATALINA_BASE/shared/classed,$CATALINA_BASE/shared/lib,都由这个加载器加载。
WebappX:对每个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
北大青鸟推荐:Java精选笔试题(含答案解析)
北大青鸟推荐:Java精选笔试题(含答案解析)如果你是计算机专业出生,但是还没有找到工作的话,你就得补补技术了,一些关于面试、笔试的题要多刷一刷。有可能你知道答案,但是由于语言组织能力有所欠缺,所以面试官的印象不是很好,下面分享一些Java精选的鄙视题,希望对面试这者有帮助。 1,volatile关键字是否能保证线程安全?() 答案:否 volatile关键字用在多线程同步中,可保证读取的可见性,JVM只是保证从主内存加载到线程工作内存的值是最新的读取值,而非cache中。但多个线程对volatile的写操作,无法保证线程安全。 假如线程1,线程2 在进行read,load 操作中,发现主内存中count的值都是5,那么都会加载这个最新的值,在线程1对count进行修改之后,会write到主内存中,主内存中的count变量就会变为6;线程2由于已经进行read,load操作,在进行运算之后,也会更新主内存count的变量值为6;导致两个线程及时volatile关键字修改之后,还是会存在并发的情况。 2,下面哪个流类属于面向字符的输入流( ) A、BufferedWriter B、FileInputStream C、ObjectInputStream D、InputStreamReader 答案:D Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式。
面向字节的操作为以8位为单位对二进制的数据进行操作,对数据不进行转换,这些类都是InputStream和OutputStream的子类。 面向字符的操作为以字符为单位对数据进行操作,在读的时候将二进制数据转为字符,在写的时候将字符转为二进制数据,这些类都是Reader和Writer的子类。 3,Java能不能不通过构造函数创建对象() A、能 B、不能 答案:A Java创建对象的几种方式: (1) 用new语句创建对象,这是最常见的创建对象的方法。 (2) 运用反射手段,调用https://www.360docs.net/doc/dd13318224.html,ng.Class或者https://www.360docs.net/doc/dd13318224.html,ng.reflect.Constructor类的newInstance()实例方法。 (3) 调用对象的clone()方法。 (4) 运用反序列化手段,调用java.io.ObjectInputStream对象的readObject()方法。 (1)和(2)都会明确的显式的调用构造函数;(3)是在内存上对已有对象的影印,所以不会调用构造函数;(4)是从文件中还原类的对象,也不会调用构造函数。 4,下列哪个叙述是正确的() A.子类继承父类的构造方法。 B.abstract类的子类必须是非abstract类。 C.子类继承的方法只能操作子类继承和隐藏的成员变量。 D.子类重写或新增的方法也能直接操作被子类隐藏的成员变量。 答案:C 子类是不继承父类的构造方法的,而是必须调用其父类的构造方法。
Java API 试题
永隆 JAVA笔试题 一、选择题 1、关于Java 类的加载过程,下面哪些描述是正确的() A、在 Java 中,有四种类型的类加载器:BootStrapClassLoader、ExtClassLoader、AppClassLoader 以及用户自定义的ClassLoader。//Extension ClassLoader, System ClassLoader+用户自定义的classloader B、使用 new 关键字创建类实例时,其实就显示地包含了类的加载过程 C、在 Java 中,类的实例化流程分为两个部分:类的加载和类的实例化。类的加载又分为显式加载和隐式加载。 D、Class.forName 来加载类时,是通过 ExtClassLoader进行加载的。 //system classLoader 加载 2、关于HashMap的实现机制,下面哪些描述是正确的() A、HashMap中key-value 当成一个整体进行处理,系统总是根据数组的坐标来获得key-value 的存储位置。//没有存储顺序,无下标之说! B、HashMap基于哈希表的 Map 接口的实现,允许使用 null 值和 null 键。 C、如果HashMap中,如果Key的hash相同的话,HashMap将会出错。//会替换相应的value D、HashMap每次容量的扩增都是以2的倍数来增加。//大约获得2倍的桶数! 3、下面的代码执行输出正确的是() 1. public class test( 2. public int aMethod()[ 3. static int i=0; 4. i++; 5. return I; 6. ) 7. public static void main (String args[]){ 8. test test = new test(); 9. test.aMethod(); 10.int j = test.aMethod(); 11.System.out.printIn(j); 12.] 13.} A. 编译错误 B. 编译成功,打印出是“0” C. 编译成功,打印出是“1” D. 编译成功,打印出是“2” A 4、如何获取下面表单 select