1. Official feature
200:the Modular JDK
201:modular Source Code
220:modular Run-time Images
260:encapsulate most Internal APIs
261:module System
282:jlink:the Java Linker
2. Creating Background and meaning
When it comes to Java 9, the first thing people think about is the Jigsaw project. As we all know, Java has been developed for more than 20 years (originally released in 95), and Java and related ecosystems are becoming richer and more exposed to some problems:
①java the expansion and bloat of the operating environment. Each time the JVM starts, at least 30~60MB memory is loaded, The main reason is that the JVM needs to load Rt.jar, regardless of whether the class is ClassLoader loaded, the first step the entire jar will be loaded into memory by the JVM (and the module can be loaded according to the needs of the module to run the required class)
② as the code base grows, the odds of creating a complex, intertwined "spaghetti code" grow exponentially. The cross-reliance of different versions of a class library leads to headaches that hinder the improvement of Java development and operational efficiency.
③ is hard to really encapsulate code, and the system does not have a clear idea of the dependencies between different parts (that is, JAR files). Each public class can be accessed by any other public class under the Classpath, which leads to an unintended use of APIs that are not publicly accessible.
The ④ classpath itself also has a problem: how do you know that all the jars you need are already there, or are there duplicates?
At the same time, due to the constraints of compatibility and other aspects of Java to make a drastic innovation more and more difficult, Jigsaw from the Java 7 phase of the preparation, Java 8 stage of a lot of work, finally landed in Java 9, a awaited began to mean.
The workload and difficulty of the Jigsaw project (later renamed Modularity) went far beyond the initial planning. The JSR 376 Java Platform Modular System (Jpms,java Platform module systems) is the core of the Jigsaw project, and its main body is decomposed into 6 JEP (JDK enhancement proposals).
As one of the biggest features of the Java 9 platform, developers no longer have to fret about the expanding Java platform as the Java platform's modular system falls, for example, you can use the Jlink tool to customize the runtime environment as needed. This is of great importance for large applications, such as container scenarios or complex dependencies with large numbers of mirrors.
In essence, the concept of modules (module) is actually wrapped outside the package, that is, to use a module to manage the package, by declaring a package exposure, do not declare the default is hidden. Therefore, Modularity makes the code organization more secure because it can specify which parts can be exposed and which are hidden.
3. Design concept
The module is independent, the complexity is simple
Modularity (in the form of a Java Platform Module system) divides the JDK into a set of modules that can be combined at compile time, at run time, or at build time.
4. Achieving the goal
The main purpose is to reduce the overhead of memory
Only necessary modules, not all JDK modules, simplify the development and maintenance of a wide variety of libraries and large applications
Improve the Java SE platform to accommodate computing devices of different sizes
Improve its security, maintainability, and performance
5. Examples of Use
Note: The IntelliJ idea 2017.3 supports the modularity feature, where this development environment is selected.
The module will consist of a common class and a new module declaration file (Module-info.java). The file is at the top level of the Java code structure, which explicitly defines what dependencies our modules need and which modules are used externally. All packages not mentioned in the EXPORTS clause are encapsulated in the module by default and cannot be used externally.
The Moduletest class in the Java 9demo module is used as follows:
corresponding to the SRC in the Java 9demo module
Under Create Module-info.java file:
requires : Indicates a dependency on other modules.
The class person is provided under the specified package for the Java9test module:
To invoke the structure in the Java9test module in the Java9demo module, you need to declare it in the Module-info.java of Java9test:
exports : Controls which packages can be accessed by other modules. All packages that are not exported are encapsulated in the module by default.
6. Additional Information
For more information on programming Java 9 modules, please refer to a book: "Java 9 modularity" in more detail, describes the current Java on the management of the jar since the chaos, the introduction of modularity after the change will be very obvious difference.
Still Silicon Valley interviewer Song Hongkang
Links: https://www.jianshu.com/p/5d62f644f358
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
JAVA9 new Features-modular system: Jigsaw and Modularity