java - 冲突的 JAR : shouldn't WEB-INF/lib take precedence over all others places?

标签 java jakarta-ee tomcat servlets classloader

我的部署环境很乱:jar文件散落在系统各处,我无法控制。例如,我的 webapp 使用最新版本的 Javamail API,但似乎在 $JAVA_HOME/jre/lib/ext 中有同一个库的多个副本(显然所有旧版本和不同版本)。这同样适用于数据库供应商的驱动程序,更不用说 $CATALINA_HOME/lib 了。 我无法清理任何东西,但显然我需要确保我的应用程序获得其依赖项的最低要求版本,因此我将所有必需的 JAR 放在 WEB-INF/lib。根据 Servlet 规范和 Tomcat docs :

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

所以它应该可以解决我的问题。但随后同一位医生说:

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

  1. Bootstrap classes of your JVM
  2. System class loader classes (described above)
  3. /WEB-INF/classes of your web application
  4. /WEB-INF/lib/*.jar of your web application
  5. Common class loader classes (described above)

实际上 /WEB-INF/lib 中的类是系统类之后加载的。这确实是预期的行为吗?你不觉得Tomcat文档自相矛盾吗?您知道任何可以保证我可以获得正确依赖项的 servlet 容器吗?我怎么解决这个问题?

最佳答案

如果 ext 文件夹被污染,那你基本上就不走运了。

您唯一可以做的就是将您需要的所有代码(包括 jar)重新打包到一个单独的包层次结构中,并与您的应用程序一起打包。这意味着 com.foo.Bar 将变成 org.raffaele.com.foo.bar 或类似的。然后仅使用这些新名称。

注意反射和带有类名的字符串,但这是可以做到的。

我不知道是否有一个工具可以完全自动化这个过程。

关于java - 冲突的 JAR : shouldn't WEB-INF/lib take precedence over all others places?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9446848/

相关文章:

java - 调用 Activity 的另一种方法。?

java - hibernatetemplate 清除ehcache?

Java 使用套接字使用 100% 的 CPU

java - 设置 maxHttpHeaderSize 属性 tomcat maven 插件

java - Hibernate 中黑白鉴别器值和鉴别器列的区别?

java - 正则表达式从任意货币格式中提取美分值

java - Maven:无法检索pluginorg.apache.maven.plugins:maven-install-plugin:2.4或其依赖项之一无法解析

java - 调试 osgi 类加载器问题

java - 由 : org. 引起的 hibernate.MappingException: Could not determine type for

javascript - 尝试将文件上传到tomcat服务器时连接不断重置