如何在 Java EE 7 应用程序模块中正确使用 Class-Path 机制

How to properly use Class-Path mechanism in Java EE 7 application modules

我很好奇如何在 portable Java EE 7 多模块应用程序中正确使用 Class-Path 机制。

EJB 模块必须引用第 3 方库,放置在 EAR 的 lib 文件夹中,在其 MANIFEST.MF Class-路径中?

对我来说,似乎不需要对它们的显式引用,因为 Java EE 规范(EE.8.4.1 "Assembling a Java EE Application",项目 3.e)指出:

Make sure that each component in the application properly describes any dependencies it may have on other components in the application. A Java EE application should not assume that all components in the application will be available on the class path of the application at run time. Each component might be loaded into a separate class loader with a separate namespace. If the classes in a JAR file depend on classes in another JAR file, the first JAR file should reference the second JAR file using the ClassPath mechanism. A notable exception to this rule is JAR files located in the WEB-INF/lib directory of a web application. All such JAR files are included in the class path of the web application at runtime; explicit references to them using the Class-Path mechanism are not needed. Another exception to this rule is JAR files located in the library directory (usually named lib) in the application package.

但是当我在谷歌搜索这个问题时,我发现建议将对第 3 方库的引用添加到 Class-EJB 模块的路径。这让我有点困惑。

我将不胜感激!

好吧,这取决于您的依赖项的加载方式。

如果它们在 ear 级别加载(在所有 ejb 模块之间共享),则无需在 ejb-jar 清单中指定任何内容(参见 Parent First delegation model),否则如果您需要模块依赖项要特定于模块(并且如此隔离),您必须在清单中明确引用它们。

这是一篇好文章:http://www.developer.com/java/ejb/article.php/10931_3670496_2/Packaging-EJB-3-Applications.htm