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

查看:32
本文介绍了从 .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

Bunny.classWEB-INF/classes/... 中是唯一一个与 acme 中的类具有相同名称/路径的类.罐.

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 会在 .jar 中的 .jar 之前加载类吗?em>WEB-INF/lib/ 目录与否?

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?

推荐答案

此答案不正确但无法删除,因为它是已接受的答案.请参阅下面@Sajeev 的回答.

This answer was incorrect but cannot be deleted because it is the accepted answer. See @Sajeev's answer below.

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

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