定义
同其他服务器应用一样,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%incatalina.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/classes
directory of your web application, plus classes and resources in JAR files under the/WEB-INF/lib
directory 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