shared/classes 和 war 文件下的 Java Class - 在 tomcat 中加载的优先级

Jave Class under shared/classes and war file - priority of loading in tomcat

有没有人对以下场景有很好的理解?

A class 可以部署在 tomcat 的不同位置。它可以是简单的 .class 或打包在一个 jar 文件中。我在这里列出这些选项:

阿帕奇-tomcat-6.0.35\shared\classes
阿帕奇-tomcat-6.0.35\shared\lib
阿帕奇-tomcat-6.0.35\lib
阿帕奇-tomcat-6.0.35\webapps\examples\WEB-INF\classes
apache-tomcat-6.0.35\webapps\examples\WEB-INF\lib

如果在所有这些位置都部署了相同的class(.class在classes文件夹层次结构中或打包在jar文件中并复制到*\lib\层次结构下) ,tomcat 的 class 加载程序将使用什么规则来识别它最终使用的 class?

这个解释的很好...

http://www.mulesoft.com/tcat/tomcat-classpath#how-it-differs

注意

In Tomcat 5.x, a "shared" loader was responsible for loading classes to be shared between applications, located in the directory $CATALINA_HOME/shared/lib. This was abandoned in Tomcat 6, to steer users towards simply replicating shared dependencies in each of the dependent Contexts, for improved portability. This loader is also replaced with the Common loader.

因此,如果您使用的是 Tomcat 6,"shared" 可能无法正常工作。

他,

-布雷特

经过更多研究,我找到了答案:

来自tomcat官方documentation site
因此,从Web应用程序的角度,class或资源加载在以下存储库中查找,顺序为

Bootstrap class 你的 JVM
系统 class 加载程序 classes(如上所述)
/WEB-INF/classes 您的 Web 应用程序
/WEB-INF/lib/*.jar 你的 web 应用程序
Common class loader classes(如上所述)

“通用 class 加载程序”搜索的位置由 $CATALINA_BASE/conf/catalina.properties 中的 common.loader 属性 定义。这是我们 enable/define 共享位置的地方; shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar

而上面的顺序说明了逻辑。