从 lib 目录加载 jar 文件的顺序 [英] Order of loading jar files from lib directory

查看:40
本文介绍了从 lib 目录加载 jar 文件的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下从 Tomcat 中的 lib 目录加载 jar 文件的顺序?是按字母顺序吗?随意?还是其他顺序?

Could anyone explain the order in which jar files are loaded from the lib directory within Tomcat? Is it alphabetically? Randomly? Or some other order?

推荐答案

都在 Tomcat 的类加载方法.它不一定按字母顺序排列.如果您观察到这种行为,并且您打算让您的 web 应用程序在服务器之间保持可移植性,则应该绝对不依赖它.比如Tomcat 6巧合"下单了,而Tomcat 8却没有.

It's all described in Tomcat's ClassLoading HOW-TO. It's not necessarily in alphabetic order. If you observed that behaviour, it should absolutely not be relied upon if you intend to keep your webapp portable across servers. For example, Tomcat 6 "coincidentally" orders it, but Tomcat 8 does not.

总结一下,加载顺序如下:

Summarized, the loading order is as follows:

  1. 引导程序/系统(JRE/lib,然后是 server.loader)
  2. webapp 库(WEB-INF/classes,然后是 WEB-INF/lib)
  3. 常用库(common.loader,然后是Tomcat/lib)
  4. webapp 共享库 (shared.loader)
  1. bootstrap/system (JRE/lib, then server.loader)
  2. webapp libraries (WEB-INF/classes, then WEB-INF/lib)
  3. common libraries (common.loader, then Tomcat/lib)
  4. webapp-shared libraries (shared.loader)

如果您想保证 JAR X 在 JAR Y 之后加载,那么您需要将 JAR X 放在 稍后 出现的位置之一上面的列表.

If you would like to guarantee that JAR X is loaded after JAR Y, then you'd need to put JAR X in one of the places which appears later in the listing above.

但是也有例外,在 tomcat 文档中提到

最后,Web 应用程序类加载器将始终首先为 Tomcat 实现的规范(Servlet、JSP、EL、WebSocket)委托 JavaEE API 类.Tomcat 中的所有其他类加载器都遵循通常的委托模式.

Lastly, the web application class loader will always delegate first for JavaEE API classes for the specifications implemented by Tomcat (Servlet, JSP, EL, WebSocket). All other class loaders in Tomcat follow the usual delegation pattern.

这意味着如果一个 webapp 包含任何 JavaEE 类(javax.*),那么它会被 tomcat 忽略.

That means if a webapp contains any JavaEE classes (javax.*), then it will be ignored by tomcat.

对于每个加载器,JVM 只在需要导入/执行时按顺序加载类,但尚未加载.

For each loader, the classes are just loaded by the JVM in the order whenever they needs to be imported/executed and are not loaded yet.

这篇关于从 lib 目录加载 jar 文件的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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