[Tomcat] Tomcat的classloader
定义
同其他服务器应用一样,tomcat安装了各种classloader(classes that implement java.lang.ClassLoader)
Bootstrap
|
System
|
Common
/ \
Catalina Shared
/ \
Webapp1 Webapp2 ...
/ /
Jasper1 Jasper2
- Bootstrap: contains the basic runtime classes provided by the Java Virtual Machine, plus any classes from JAR files present in the System Extensions directory (
$JAVA_HOME/jre/lib/ext). Note: some JVMs may implement this as more than one class loader, or it may not be visible (as a class loader) at all. - System: 加载系统环境变量
CLASSPATH 指定的Jars. 所有加载的类对Tomcat内部classes和web applications可见。但是tomcat脚本($CATALINA_HOME/bin/catalina.sh或%CATALINA_HOME%\bin\catalina.bat)会完全忽略掉CLASSPATH,只加载%CATALINA_HOME%/bin目录下的bootstrap.jar、tomcat-juli.jar、commons-daemon.jar。除非通过$CATALINA_HOME/bin/setenv.sh指定。 - Common: 加载额外的Jars,所有加载的类对Tomcat内部classes和web applications可见。
- WebAppX: A class loader is created for each web application that is deployed in a single Tomcat instance. All unpacked classes and resources in the
/WEB-INF/classesdirectory of your web application, plus classes and resources in JAR files under the/WEB-INF/libdirectory of your web application, are made visible to this web application, but not to other ones.
Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:
- Bootstrap classes of your JVM
- /WEB-INF/classes of your web application
- /WEB-INF/lib/*.jar of your web application
- System class loader classes (described above)
- Common class loader classes (described above)
If the web application class loader is configured with <Loader delegate="true"/> then the order becomes:
- Bootstrap classes of your JVM
- System class loader classes (described above)
- Common class loader classes (described above)
- /WEB-INF/classes of your web application
- /WEB-INF/lib/*.jar of your web application
配置
JVM Classloader
首先,你需要了解一下JVM的Classloader机制(详细请自行google之)。
简而言之,JVM的classloader加载继承关系分为BootstarpClassLoader --> ExtClassLoader --> SystemClassLoader,应用的WebAppClassLoader继承自SystemClassLoader,在加载具体某个类时,一般会先委托给父类ClassLoader,当父类ClassLoader无法加载成功时,才会再由子类ClassLoader尝试加载,这就是所谓的delegate机制。
catalina.properties
其次,Tomcat在jvm的ClassLoader机制上增加了几个继承层次。
SystemClassLoader --> CommonClassLoader -->(ServerClassLoader | SharedClassLoader --> WebAppClassLoader)。
CommonClassLoader用来加载${CATALINA_HOME}/conf/catalina.properties中common.loader配置目录下的类文件,一般是用来加载${CATALINA_HOME}/lib下的文件。该loader加载的类为tomcat服务器和tomcat下面的所有webApp所共享。
ServerClassLoader用来加载${CATALINA_HOME}/conf/catalina.properties中server.loader配置目录下的类文件,一般是用来加载${CATALINA_HOME}/server下的文件。该loader加载的类为tomcat服务器所独有核心类,tomcat下面的WebApp无法访问。
SharedClassLoader用来加载${CATALINA_HOME}/conf/catalina.properties中shared.loader配置目录下的类文件,一般是用来加载${CATALINA_HOME}/shared下的文件。该loader加载的类为tomcat下面的所有webApp所共享。
WebAppClassLoader用来加载${CATALINA_HOME}/webapps/目录下每个WebApp应用的/WEB-INF/class,/WEB-INF/lib的类文件,每个WebApp对应一个WebAppClassLoader,用来加载其所需要的类文件。
Delegate
最后,说一下delegate配置的意义。
True,表示tomcat将遵循JVM的delegate机制,即一个WebAppClassLoader在加载类文件时,会先递交给SharedClassLoader加载,SharedClassLoader无法加载成功,会继续向自己的父类委托,一直到BootstarpClassLoader,如果都没有加载成功,则最后由WebAppClassLoader自己进行加载。
False,表示将不遵循这个delegate机制,即WebAppClassLoader在加载类文件时,会优先自己尝试加载,如果加载失败,才会沿着继承链,依次委托父类加载。
在此说一下配置为False需要注意的问题:一旦配置为False,如果你在WebApp中自己定义了一个java.lang.String,则这个String类会有可能覆盖掉jdk中的String类,这也许不是你想要的结果。另外对于多个WebApp公用jar包,你可能会放到${CATALINA_HOME}/shared目录中共享,但是一不小心在应用的/WEB-INF/lib中也包含了一个同名的但版本不一致的jar的话,这就有可能会导致很多奇怪的问题。
Reference
- https://segmentfault.com/q/1010000000155690
- http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
[Tomcat] Tomcat的classloader的更多相关文章
- Tomcat学习之ClassLoader
Tomcat学习之ClassLoader 2012-09-04 22:19 8993人阅读 评论(4) 收藏 举报 分类: WEB服务器(13) 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- [tomcat] tomcat+nginx 负载均衡配置
首先下载,安装tomcat. 修改tomcat端口,修改server.xml: 1.修改tomcat端口(默认8080) <Connector port="8383" pro ...
- Tomcat——Tomcat使用详解
Tomcat简介 官网:http://tomcat.apache.org/ Tomcat GitHub 地址:https://github.com/apache/tomcat Tomcat是Apach ...
- 一个简单的dos脚本, svn 获取代码 - Tomcat 备份 - Maven 编译 - 停止/启动Tomcat - Tomcat站点 发布
获取最新代码 svn update --username %SVN_USER% --password %SVN_PASSWORD% >> "../%LOG_FILE%" ...
- [Tomcat] Tomcat远程调试
如何用eclispe远程调试tomcat 关键步骤: 1)修改启动脚本startup.bat 复制startup.bat为startup-debug.bat,然后打开startup-debug.bat ...
- 性能测试中用LambdaProbe监控Tomcat Tomcat和Probe的配置
转载:http://bbs.51testing.com/thread-90047-1-1.html 性能测试中用LambdaProbe监控TomcatLambdaProbe 是一款强大的免费开源工具, ...
- [tomcat] tomcat简析(一)
1.Tomcat的顶层结构 Tomcat中最顶层的容器叫Server,代表整个服务器,Server中包含至少一个Service,用于 具体提供服务. Service主要包含两部分:Connector和 ...
- [tomcat]tomcat 9.0.x 控制台中文乱码解决办法
根本原因,tomcat 输出的东西,与cmd控制台或者IDE控制台编码不一致. 修改tomcat输出内容的编码,%CATALINA_HOME%/conf/logging.properties 9.0. ...
- tomcat:tomcat的OutOfMemoryError解决
最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植到oracle,首先把jdbc的连接指向 mysql,打包放到tomcat里面,可以跑起来,没有问题,可是当把jdbc连接指向oracle ...
随机推荐
- String构造器中originalValue.length>size 发生的情况
最近在看Jdk6中String的源码的时候发现String的有个这样的构造方法,源代码内容如下: public String(String original) { int size = origina ...
- [iOS]技巧集锦:UICollectionView在旋转屏幕后Cell中的约束不起作用或自动布局失效
这似乎是iOS的一个BUG(ref: stackoverflow的大神们讲的) 解决方案 在继承自UITableViewCell的子类中的init方法中加入如下设置: self.contentView ...
- ubuntu 安装 vmware 12
安装VMware Workstation 12 ubuntu15.10安装VMware Workstation12的步骤如下: 1.在 https://download3.vmware.com/sof ...
- 搭建php环境哪家强
http://www.bubuko.com/infodetail-791030.html
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Ubuntu下git的安装与使用
Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...
- js版面向对象图片放大镜
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>放 ...
- 详解mysql如何配置远程链接,解决各种连接问题
在服务器上面我们经常需要去使用mysql,有些童鞋刚刚配置好了服务器,想在本地的一些图形化软件去连接mysql得到更直观的表格显示,此时很可能不允许连接,为了探究为什么连接失败,在这里我会对mysql ...
- Java并发_volatile实现可见性但不保证原子性
读后感 介绍了volatile实现可见性的基本原理 介绍了volatile不能实现原子性的示例,volatile复合操作不能实现原子性,读取值后在自增前改值可能被其它线程读取并修改,自增后刷新值可能会 ...
- 【BZOJ 4582】【Usaco2016 Open】Diamond Collector
http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...