首页   快速返回

tomcat8.5类加载机制     所属分类 tomcat
https://tomcat.apache.org/tomcat-8.5-doc/class-loader-howto.html



双亲委托

when a class loader is asked to load a particular class or resource, 
it delegates the request to a parent class loader first, 
and then looks in its own repositories only if the parent class loader(s) cannot find the requested class or resource. 

加载类时先委托给父加载器加载,加载不到再自己加载

      Bootstrap
          |
       System
          |
       Common
       /     \
  Webapp1   Webapp2 ...
  

Bootstrap 
This class loader 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). 

System 
This class loader is normally initialized from the contents of the CLASSPATH environment variable. 
All such classes are visible to both Tomcat internal classes, and to web applications.

the standard Tomcat startup scripts (bin/catalina.sh ) totally ignore the contents of the CLASSPATH environment variable itself,
 
# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=

bin/bootstrap.jar 
Contains the main() method that is used to initialize the Tomcat server, 
and the class loader implementation classes it depends on.

bin/tomcat-juli.jar
Logging implementation classes



启动脚本忽略classpath 设置, 需要的话使用  bin/setenv.sh  设置



 
Common 
This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications.

application classes should NOT be placed here. 
应用类不应该放在这里

The locations searched by this class loader are defined by the common.loader property in $CATALINA_BASE/conf/catalina.properties

搜索路径由 catalina.properties 里的 common.loader  定义

common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"


WebappX
web 应用加载器 
/WEB-INF/classes
/WEB-INF/lib

the web application class loader diverges from the default Java delegation model 
(in accordance with the recommendations in the Servlet Specification, version 2.4, section 9.7.2 Web Application Classloader). 
When a request to load a class from the web application's WebappX class loader is processed, 
this class loader will look in the local repositories first, instead of delegating before looking. 

web应用程序类加载器与默认的Java委托模型不同(根据Servlet规范2.4版9.7.2节中的建议)。
web应用类加载器加载类时,首先查看本地库,而不是委托给父加载器加载。






tomcat 类加载顺序

Bootstrap classes of your JVM
/WEB-INF/classes of your web application
/WEB-INF/lib/*.jar of your web application
System class loader classes  
Common class loader classes 


这个加载顺序可以配置

contex.xml中添加 Loader delegate="true" 
先委托给父加载器加载 ,加载顺序变为

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



Starting with Java 1.4 a copy of JAXP APIs and an XML parser are packed inside the JRE. 
This has impacts on applications that wish to use their own XML parser.



In old versions of Tomcat, you could simply replace the XML parser in the Tomcat libraries directory 
to change the parser used by all web applications. 
However, this technique will not be effective when you are running modern versions of Java, 
because the usual class loader delegation process will always choose the implementation inside the JDK in preference to this one.

在Tomcat的旧版本中,可以简单地替换Tomcat libraries目录中的XML解析器,更改所有web应用程序使用的解析器。
但是,运行现代版本的Java时,这种技术将不会有效,
因为通常的类加载器委托过程总是优先选择JDK中的实现。


Java <= 8 supports a mechanism called the "Endorsed Standards Override Mechanism" 
to allow replacement of APIs created outside of the JCP (i.e. DOM and SAX from W3C). 
It can also be used to update the XML parser implementation. 
For more information, see: http://docs.oracle.com/javase/1.5.0/docs/guide/standards/index.html. 
For Java 9+, use the upgradeable modules feature.

Endorsed Standards Override Mechanism
认可的标准覆盖机制

Tomcat utilizes the endorsed mechanism by including the system property setting -Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS
$CATALINA_HOME/endorsed


java endorsed  覆盖系统类
-Djava.endorsed.dirs
$JAVA_HOME/jre/lib/endorsed
java.lang 中的类不能被覆盖

Java9 不支持 -Djava.endorsed.dirs

Endorsed standards and standalone APIs in modular form will be supported via the concept of upgradeable modules.




更复杂的类加载器结构
A more complex class loader hierarchy may also be configured.
conf/catalina.properties
server.loader  
shared.loader


Bootstrap
      |
    System
      |
    Common
     /  \
Server  Shared
         /  \
   Webapp1  Webapp2 ...
   
   
server loader 对 webapps 不可见


The Server class loader is only visible to Tomcat internals and is completely invisible to web applications.

The Shared class loader is visible to all web applications and may be used to shared code across all web applications. 
However, any updates to this shared code will require a Tomcat restart.

上一篇     下一篇
rust特点及安装

maven命令行运行main方法

tomcat8.5压测记录

tomcat8.5嵌入式启动

tomcat8.5架构概述

tomcat8.5启动过程远程调试