A preliminary study of the modularization of Java 9

Source: Internet
Author: User
Tags modifier java se

The most important feature in Java 9, no doubt, is modular (module), which has long relied on the structure of the JRE to transform itself into module-based components, which are, of course, very different from the previous use of Java 9 development.

Problems of JAVA8 or earlier systems
    1. Jar files, such as jar files such as Rt.jar, are too large to be used in small devices and applications.
    2. Because the JDK is too large, our app or device does not support a better platform.
    3. Because the modifier is public, everyone can access it, so the current Java system is not very closed.
    4. Because the JDK,JRE is too large, it is difficult to test and maintain the application.
    5. Because of the public relationship, Java is more open. Unavoidable access to some of the less important APIs, such as Sun,. internal.*, etc.
Features of the JAVA9 module system
    1. The JDK was isolated in Java SE 9, Jre,jar, and so on for smaller modules. So we can easily use any of the modules we want. Therefore, it is very easy to reduce Java applications to small devices.
    2. Easier to test and maintain.
    3. Support for better platforms.
    4. Public is no longer just public. Very strong containment is now supported (don't worry, we'll explain it in a few examples behind).
    5. We can no longer access internal non-critical APIs.
    6. Modules can be very safe to help us hide the internal details that we don't want to expose, and we can get better security.
    7. The application will be very small, because we can use only the modules we want.
    8. Loose coupling between components becomes very easy.
    9. It is easier to support the sole responsibility principle (SRP).
Directory structure

There was a large rack package in the previous JRE, with 62M Rt.jar in the JDK8, which must be carried even if the simplest helloworld were to be run.

Directory of Jdk9

We found that there is no JRE file in the Jdk9, there is no rt.jar such a large rack package, but it has a new file Jmods, modules are placed in the Jmods folder.

There are currently 98 modules.

Related properties of module

module-info.javaYou can manage the module for this project by creating a new file under/main/java/in the home directory.

module M {}
Module naming

Also known as module description file
The module naming needs to be guaranteed single and can be used in reverse domain name mode, for example com.wuwii.xxx.xxx , this module will export the package com.wuwii .

In JDK 9, open, module, requires, transitive, exports, opens, to, uses, provides, and with are restricted keywords. They have special meaning only when they appear in the module declaration. They can be used as identifiers elsewhere in the program.

For example, you can declare a module variable in your program.

Access rights

The relationship between modules is known as readability (readability), which indicates whether a module can find the module file and read it into the system (note: It does not represent a type that can be accessed). In the actual code, one type is called accessibility (Accessible) for another type of invocation, which means that the type can be used; accessibility is premised on readability, in other words, existing modules are readable, and then further detection of accessibility (security).
The export statement exports the specified package of the module to all modules or to the list of named modules at compile time and runtime. Two of its forms are as follows:

exports <package>;exports <package> to <module1>, <module2>...;

The following is an example of a module that uses an export statement:

module java.xml.ws {……    exports com.oracle.webservices.internal.api.databinding to        jdk.xml.ws;    exports com.sun.xml.internal.ws.addressing to        jdk.xml.ws,        java.xml.bind;……}

Open statements allow reflection on all modules to access the specified package or list of modules specified at run time. Other modules can use reflection to access all types in the specified package and all members of those types (private and public). Open statements take the following form:

opens <package>;opens <package> to <module1>, <module2>...;

Tips
Compare the export and open statements. The export statement allows access to the public APIs for the specified package only at compile time and at run time, while open statements allow reflection to be used at run time to access all types of public and private members in the specified package.

module N {    exports M;    opens M;}

You will encounter three phrases when reading about the module:

    • Module M export Package p
    • Module M open Package Q
    • Module M contains package r

The first two phrases correspond to the export statements and open statements in the module. The third phrase means that the package r contained in the module is neither exported nor open. In the early design of the modular system, the third case is called "module M hidden Packet r".

declaring dependencies

The (Require) statement is required to declare the dependencies of the current module with another module. A "need n" statement in a module named M indicates that the module m depends on (or reads) the module N. Statements have the following form:

staticstatic <module>;
    • The static modifier in the Require statement indicates that the dependency at compile time is mandatory, but is optional at run time.
    • The requires static N statement means that module m depends on module N, module n must appear at compile time to compile module m, and module n is optional at run time.
    • The transitive modifier in the Require statement causes the other modules that depend on the current module to have implicit dependencies.
    • Assuming that there are three modules P,q and R, assuming that module Q contains requires transitive R statements, if the module p contains a requires Q statement, this means that the module p implicitly depends on the module R.
Configure the Service

Java allows service provider mechanisms separated by service providers and service users. JDK 9 allows the use of statements (uses statement) and provide statements (provides statement) to implement their services.

Use the statement to specify the name of the service interface, which the current module will discover and load with the Java.util.ServiceLoader class. The format is as follows:

uses <service-interface>;

Examples of using statements are as follows:

module M {    uses com.jdojo.prime.PrimeChecker;}

Com.jdojo.PrimeChecker is a service interface whose implementation classes will be provided by other modules. Module M will use the Java.util.ServiceLoader class to discover and load the implementation of this interface.

Provides one or more service provider implementation classes that specify the service interface for a statement. It takes the following form:

provides <service-interface>    with <service-impl-class1>, <service-impl-class2>...;

The same module can provide service implementations that can discover and load services. Modules can also discover and load a service and provide implementations for another service. Here is an example:

module P {    uses com.jdojo.CsvParser;    provides com.jdojo.CsvParser        with com.jdojo.CsvParserImpl;    provides com.jdojo.prime.PrimeChecker        with com.jdojo.prime.generic.FasterPrimeChecker;}
Summarize

It is important to note that not only the 98 modules built into the JDK, reference Maven's third-party racks, also require module,
More logs, if used

    /**     * logger     */    private static final Logger LOGGER = LoggerFactory.getLogger(LearnSoap.class);

Configuration
Required in module-info.java configuration:

requires slf4j.api;

Packages in Java have been used as containers for types. The application consists of several jars placed on the classpath. The package is a container of type and does not enforce any accessibility boundaries. The accessibility of a type is built into a type declaration that uses modifiers. If the package contains an internal implementation, it cannot prevent other parts of the program from accessing the internal implementation. The classpath mechanism uses a linear search type when the type is used. This results in another problem of receiving errors at run time when the type is missing in the deployed jar--sometimes for a long time after the application is deployed. These problems can be categorized into two types: encapsulation and configuration.

JDK 9 introduces a modular system. It provides a way to organize Java programs. It has two main goals: a powerful package and a reliable configuration. Using a modular system, the application consists of modules, which are named as collections of code and data. The module uses its declaration to control the parts of the module that can be accessed by other modules. A module that accesses parts of another module must declare a dependency on the second module. Controlling access and claims depends on the basis for a strong package. Resolves a module's dependencies when the application starts. In JDK 9, if a module relies on another module and the second module is lost when the application is run, it will receive an error at startup instead of a certain time after the application is run. This is a reliable base configuration.

Use module declarations to define modules. The source code for the module is usually stored in a file named Module-info.java. A module is compiled into a class file, usually named Module-info.class. The compiled module declaration is called a module descriptor. Module declaration does not allow the specification of a module version. However, a module version can be added to a module descriptor, such as a jar tool that packs a module into a jar.

Use the module keyword to declare a module, followed by its name. Module declarations can use five types of module statements: Exports,opens,require,uses and provide. The export statement exports the specified package of the module to all modules or to the list of named modules at compile time and runtime. Open statements allow reflection on all modules to access the specified package or list of modules specified at run time, and other modules can use reflection to access all types in the specified package and all members of those types (private and public). Use statements and provide module statements to configure a module to discover service implementations and provide service implementations for a specific service interface.

Starting with JDK 9, open, module, requires, transitive, exports,opens,to,uses,provides, and with are restricted keywords. They have special meaning only when they appear in the module declaration.

Reference articles
    • Java 9 Revelation (2. Modular system)
    • New features of Java 9 come-modular

A preliminary study of the modularization of Java 9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.