JPMS/Project Jigsaw 对小型应用程序/库的好处

Benefits of JPMS/Project Jigsaw for Small Applications / Libraries

我了解 Java 平台模块系统 (JPMS) 对大型应用程序的好处,但是是否有任何理由将小型库或应用程序制作成(单个)模块?如果是这样,Modular Jar Files 是完成此任务的最佳方法,还是首选常规方法?

展望未来,模块化 v.classpath 程序是否会对性能产生影响?

直接的性能影响包括以下内容:

  • 模块化应用程序可以选择使用 jlink so that the distributable runtime is reduced in size: it only uses the modules you require. So if you don't need Swing, Corba, etc, it does not reside on disk. (more info here).
  • 模块化应用程序使用模块图进行类加载;该算法比类路径的线性搜索快得多(更多信息 here)。

这绝不保证您的应用程序会更快,但以上内容无疑具有吸引力。

从长远来看,考虑一下:

  • 虽然在 JDK 9 中处于试验阶段,但有一个 AOT 编译器(对于 Linux x64)会将 java.base 模块编译为本机代码。这可能是未来 Java 版本的前进方向。当然,这将改善未来的启动时间。 (再次,更多信息 here

If so, are Modular Jar Files the best means of accomplishing this, or is the normal approach preferred?

我不确定你所说的 "the normal approach" 是什么意思 - modular JARs 是 "normal approach" 创建模块系统将为其创建模块的工件。

[I]s there any reason to make a small library or application into a (single) module?

有,有几个:

  • 创建模块化应用程序或使用模块化库的开发人员受益于 reliable configuration,他们只能在所有依赖项都存在的情况下启动他们的应用程序 - 无论有多大,这都是一个好处application/library是。

  • 作为库设计者,您可以隐藏内部结构。虽然 strong encapsulation 仅在将模块化 JAR 放入模块路径时才有效,但模块描述符仍会在代码中记录您的意图,并允许您更肆无忌惮地更改代码。应用程序开发人员也从中受益,因为他们不会再意外地依赖不受支持的实现细节。

  • 即使在小型应用程序和库中,服务也可用于分离功能的不同方面并使代码更易于扩展。

  • jlink 只有 "real" 个模块(相对于自动模块)可以包含在运行时映像中。不提供模块化 JAR 会使您的用户失去这种可能性。

另请注意,您永远不知道哪个小应用程序或库可能会变成更大的东西。 :)

Going forward, will there be performance implications for modularized v. classpath programs?

虽然构建模块图和验证模块需要时间,但模块系统应该用改进的 class 加载策略来弥补:它记录每个包导出哪个模块并直接从该包加载类型从该模块,而不是必须扫描整个 class 路径。无论哪种方式,我都不认为这应该是决定因素。

发现此 link 内容丰富
http://www.javaworld.com/article/2878952/java-platform/modularity-in-java-9.html

Aren't JAR files modular enough?

JAR files and the deployment environment in which they operate greatly improve on the many legacy deployment conventions otherwise available. But JAR files have no intrinsic uniqueness, apart from a rarely used version number, which is hidden in a .jar manifest. The JAR file and the optional manifest are not used as modularity conventions within the Java runtime environment. So the package names of classes in the file and their participation in a classpath are the only parts of the JAR structure that lend modularity to the runtime environment.

In short, JARs are a good attempt at modularization, but they don't fulfill all the requirements for a truly modular environment. Frameworks and platforms like Spring and OSGi use patterns and enhancements to the JAR specification to provide environments for building very capable and modular systems. Over time, however, even these tools will succumb to a very unfortunate side-effect of the JAR specification JAR hell!

Classpath/JAR hell

When the Java runtime environment allows for arbitrarily complex JAR loading mechanisms, developers know they are in classpath hell or JAR hell. A number of configurations can lead to this condition.

First, consider a situation where a Java application developer provides an updated version of the application and has packaged it in a JAR file with the exact same name as the old version. The Java runtime environment provides no validation facilities for determining the correct JAR file. The runtime environment will simply load classes from the JAR file that it finds first or that satisfies one of many classpath rules. This leads to unexpected behavior at best.

Another instance of JAR hell arises where two or more applications or processes depend on different versions of a third-party library. Using standard class-loading facilities, only one version of the third-party library will be available at runtime, leading to errors in at least one application or process.

A full-featured and efficient Java module system should facilitate separation of code into distinct, easily understood, and loosely coupled modules. Dependencies should be clearly specified and strictly enforced. Facilities should be available that allow modules to be upgraded without having a negative effect on other modules. A modular runtime environment should enable configurations that are specific to a particular domain or vertical market, thus reducing the startup time and system footprint of the environment.

Modularity solutions for Java

Along with the modularity features mentioned so far, recent efforts add a few more. The following features are intended to optimize performance and enable extending the runtime environment:

Segmented source code: Source code separated into distinct, cached segments, each of which contains a specific type of compiled code. The goals of which include skipping non-method code during garbage sweeps, incremental builds, and better memory management.

Build-time enforcements: Language constructs to enforce namespaces, versioning, dependencies, and others.

Deployment facilities: Support for deploying scaled runtime environments according to specific needs, such as those of a mobile device environment