从.war文件加载类的顺序 [英] Order of class loading from a .war file

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

问题描述

我在以下场景中有关于担保的问题(如果有的话)(注意问题不是如何以不同的方式做到这一点?,问题是真的关于以下情况下的类加载顺序(为了更好地理解类加载的工作原理)。

I've got a question regarding the guarantees, if any, in the following scenario (note that the question is not "How to do this in a different way?", the question is really about class loading order in the following case (to better understand how class loading works).

这是假设的场景......有一个 .war 具有以下(部分)目录结构的文件:

Here's the hypothetical scenario... There's a .war file that has the following (partial) directory structure:

 WEB-INF/classes/com/acme/Bunny.class
 .
 .
 .
 WEB-INF/lib/acme.jar

两个 Bunny.class 文件都有导入引用其他类来自 acme.jar

Both Bunny.class files have import referencing other classes from acme.jar

WEB-INF / classes /...中的em> Bunny.class 是唯一一个与 acme.jar 。

Bunny.class in WEB-INF/classes/... is the only class that has the same name/path that a class from acme.jar.

.jar 文件 acme.jar 还包含 com.acme.Bunny (并且没有使用特殊的类加载器技巧)。

The .jar file acme.jar also contains com.acme.Bunny (and there are a no special class loader tricks used).

我明白了并且Java规范保证在程序实际使用(或故意手动加载)之前不会加载类,这就是为什么如果你在数组中填充数千个.jar的原因。 war,类加载器不会开始加载数万个类。

I understand that the Java spec guarantees that a class won't be loaded until it is actually used (or "manually class-loaded" on purpose) by the program, which is why if you stuff thousands of .jar, say, in a .war, the classloader(s) don't start classloading tens of thousands of classes.

(编辑)


但是上面例子中的
两个类的订单是
加载的顺序呢?

But what about the order in which the two classes in the examples above are loaded?

应该是措辞:


但是如何决定哪一个
加载了上面两个类?

But how is it decided which one of the two classes above is loaded?

或者类似的东西:)

有一个保证: com.acme.Bunny 应该在 com.acme ... 的任何其他类之前使用。

There's one guarantee made: com.acme.Bunny shall be used before any other class from com.acme....

基本上,在维基百科上,写道如下:

Basically, on Wikipedia, the following is written:


最复杂的JAR地狱问题
出现在情况下这个类加载系统的
的全部复杂性带来
的优势。 Java
程序不需要仅使用
单个平面类加载器,而是
可能由几个(或者,
事实,无限数量)组成嵌套,
合作的类加载器。由不同的类加载器加载的类
可能以复杂的方式进行交互,而不是完全由开发人员理解的
,导致
到无法解释的错误或错误。

The most complex JAR hell problems arise in circumstances that take advantage of the full complexity of the classloading system. A Java program is not required to use only a single "flat" classloader, but instead may be composed of several (or, in fact, an indefinite number of) nested, cooperating classloaders. Classes loaded by different classloaders may interact in complex ways not fully comprehended by a developer, leading to inexplicable errors or bugs.

所以我想知道:我能否确定 /classes/com/acme/Bunny.class 将在之前进行分类加载 WEB-INF / lib / dir中是否> .jar

So I'm wondering: can I be sure that /classes/com/acme/Bunny.class will be classloaded before the one from .jar inside the WEB-INF/lib/ dir or not?

推荐答案

这个问题可能会有所帮助。

servlet规范对此很模糊。人们期望在WEB-INF / lib之前搜索WEB-INF / classes,但似乎由servlet容器来决定。您可以确定的是,容器应该始终先加载一个容器,因此您永远不应该在同一个容器中看到这两个类。搜索路径可以可配置,具体取决于您的容器。

The servlet specification is vague on this. One would expect "WEB-INF/classes" to be searched before "WEB-INF/lib" but it seems that it's up to the servlet container to decide. All you can be sure of is that the container should consistently load one before the other, so you should never see both classes in the same container. The search path may be configurable, depending on your container.

抱歉,我不能更具体:欢迎来到多个servlet的世界容器。不要让我开始使用Websphere娱乐和游戏。

Sorry I can't be more specific: welcome to the world of multiple servlet containers. Don't get me started on Websphere fun-and-games.

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

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