如果 [WEB-INF/lib] 和 [tomcat/lib] 包含相似的 JAR 会怎样?

What will happen if [WEB-INF/lib] and [tomcat/lib] contain similar JARs?

今天我在采访中被问到,如果 Tomcat 的 libWEB-INF/libwar 文件包含相同的 jar 文件?

我今天在我的机器上测试,我没有发现任何差异。应用程序部署成功。

我已经完成了这个 similar article,如果有人知道如果 Tomcat 的 libWEB-INF/lib 会发生什么,请进一步说明包含相同的 jar 会发生什么??

WEB-INF/lib 中的罐子将被 tomcat 拾取。唯一需要注意的是核心容器 jar 将优先于 WEB-INF/lib.

中的 jar 加载

阅读:https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html#Class_Loader_Definitions

取决于 delegate 属性

我还没有尝试过这些。但它似乎取决于布尔 delegate 属性的值。

我阅读 https://tomcat.apache.org/tomcat-10.0-doc/class-loader-howto.html#Class_Loader_Definitions 的方式是这样的:

对于任何网络应用程序,都有四个 class 加载程序:bootstrap、系统、通用、网络应用程序。

  • 默认情况下,布尔 delegate 属性未指定,默认为 false
    • => class 加载程序的顺序:bootstrapwebappsystemcommon
    • => 您的网络应用程序的 JAR 获胜。
  • 当且仅当您明确指定 <Loader delegate="true"/>
    • => class 加载程序的顺序:bootstrapsystemcommonwebapp
    • => common 从 $CATALINA_BASE 和 $CATALINA_HOME
    • 获取 JAR
    • => Tomcat 的 JAR 获胜。

进一步阅读

添加信息:这是“委托”属性本身的文档。

我觉得这有点令人困惑,但似乎 Tomcat 的默认设置不遵循 Java 的默认设置。 (不确定 Java 的默认 “委托模型” 可能在哪里被定义。)

引自https://tomcat.apache.org/tomcat-10.0-doc/config/loader.html#Common_Attributes (为了强调而更改了格式。)

  • Set to true if you want the class loader to follow the standard Java2 delegation model, and attempt to load classes from parent class loaders before looking inside the web application.

  • Set to false (the default) to have the class loader look inside the web application first, before asking parent class loaders to find requested classes or resources.