WEB-INF/classes/vs WEB-INF/lib/*.jar 类路径优先级? [英] WEB-INF/classes/ vs WEB-INF/lib/*.jar in classpath priority?

查看:37
本文介绍了WEB-INF/classes/vs WEB-INF/lib/*.jar 类路径优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为tomcat webapp打包的war包含WEB-INF/classes和WEB-INF/lib/*.jar

a war packaged for a tomcat webapp contains WEB-INF/classes and WEB-INF/lib/*.jar

它们中哪个在 CLASSPATH 中具有更高的优先级?

which of them has higher priority in the CLASSPATH?

我问的原因是,我的应用程序使用 A.jar,其中包含从 aspectj 项目生成的方面;和 B.jar,它们将与 A.jar 的方面编织在一起.当项目 myapp 被编译时,它会生成许多覆盖 B.jar 中相同类的类,这些类被打包到 WEB-INF/classes 目录中.所以如果tomcat先加载WEB-INF/lib/*.jar,那么编织的aspect不会生效

the reason I'm asking is, that my application uses A.jar, which contains aspects generated from an aspectj project; and B.jar, which are to be woven with the aspects from A.jar. when the project myapp is compiled, it generates many Classes which override those same classes from B.jar, these are packaged into the WEB-INF/classes dir. so if tomcat load WEB-INF/lib/*.jar first, then the woven aspects won't take effect

推荐答案

Tomcat在类加载器上有几个文档:

Tomcat has several documents on the class loader:

引自 http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html:
(为了可读性添加了换行符)

Quote from http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html:
(line breaks added for readability)

如上所述,Web 应用程序类加载器不同于默认的 Java 2 委托模型(根据 Servlet 规范 2.3 版第 9.7.2 节 Web 应用程序类加载器中的建议).

As mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader).

当处理从 Web 应用程序的 WebappX 类加载器加载类的请求时,该类加载器将首先在本地存储库中查找,而不是先委托再查找.
有例外.作为 JRE 基类一部分的类不能被覆盖.对于某些类(例如 J2SE 1.4+ 中的 XML 解析器组件),可以使用 J2SE 1.4 认可的特性(请参阅上面的通用类加载器定义).

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.
There are exceptions. Classes which are part of the JRE base classes cannot be overriden. For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used (see the common classloader definition above).

最后,类加载器将忽略任何包含 servlet API 类的 JAR.

Last, any JAR containing servlet API classes will be ignored by the classloader.

Tomcat 5 中的所有其他类加载器都遵循通常的委托模式.

All other class loaders in Tomcat 5 follow the usual delegation pattern.

因此,从 Web 应用程序的角度来看,类或资源加载在以下存储库中,按此顺序:

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

  • JVM 的引导类
  • 系统类加载器类(如上所述)
  • /WEB-INF/classes 您的网络应用程序
  • /WEB-INF/lib/*.jar 您的网络应用程序
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/i18n/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_BASE/shared/classes
  • $CATALINA_BASE/shared/lib/*.jar
  • Bootstrap classes of your JVM
  • System class loader classes (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/i18n/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_BASE/shared/classes
  • $CATALINA_BASE/shared/lib/*.jar

所以 WEB-INF/classesWEB-INF/lib 之前先被搜索.

So WEB-INF/classes is searched first before WEB-INF/lib.

这篇关于WEB-INF/classes/vs WEB-INF/lib/*.jar 类路径优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆