Preface
As of now, the JDK version has been updated to 10. Although the life cycle of java9 is only half a year, I think the changes brought by this version are indelible. It is the first deep-seated innovation in architecture and dependency. Let’s learn more below.
Modular functions have several purposes:
1. Modular project construction
In fact, modularization itself is not difficult to understand. We used maven or gradle to build projects with too many modules. Then we can still build our modular project project in Java9. As shown in the figure:
Pay attention to the following points:
1. Please create a modular description file called module-info.java under each module
2. Configure the module dependencies in idea. Here, if our project.portal module depends on the student.service module, we can set it like this:
Find this option icon: and set it like this to add dependencies:
If you need to set dependencies for other projects, please set them in this way.
2. Implementation steps
2.1. Student.Service module
2.1.1. Write module-info.java of StudentService
Sample code:
import com.bdqn.lyrk.student.service.SecondStudentService;import com.bdqn.lyrk.student.service.api.IStudentService;/** * Modular description class, unified in the root directory of the source file of each module is: module-info.java * Common syntax structures in modularity: * * import xxxx.xxxx; * .... * * [open] module module name { * requires [static|transitive] module name; * exports package name [to module name] * providers interface name with [interface implementation class,....] * uses interface name* * } * ** @author chen.nie* @date 2018/4/18**/module student.service { exports com.bdqn.lyrk.student.service.api; provide IStudentService with SecondStudentService;}2.1.2. Define the interface
package com.bdqn.lyrk.student.service.api;public interface IStudentService { void study();}2.1.3. Define implementation classes
package com.bdqn.lyrk.student.service;import com.bdqn.lyrk.student.service.api.IStudentService;public class SecondStudentService implements IStudentService { @Override public void study() { System.out.println("second study"); }}2.2. project.portal module
2.2.1. Write module-info.java
import com.bdqn.lyrk.student.service.api.IStudentService;module project.portal { uses IStudentService; requires transitive student.service;}2.2.2. Write Main Method
package com.bdqn.lyrk.portal;import com.bdqn.lyrk.student.service.api.IStudentService;import java.util.ServiceLoader;public class Main { public static void main(String[] args) { ServiceLoader<IStudentService> studentServices = ServiceLoader.load(IStudentService.class); studentServices.findFirst().get().study(); }}After running, we can get the corresponding results:
3. Common configurations of module-info.java files
3.1. About the open keywords
open: If this keyword is loaded on the module, the class visibility under the export package through exports is the highest. We can create objects and access properties through reflection.
3.2. About exports keywords
After we define the module, we can specify which packages under the module can be accessed by other modules, and the exports keyword plays this role. We can also cooperate with to specify which modules can access the contents of the package
Syntax exports package name[to] module name
exports <package>;exports <package> to <module1>, <module2>...;
3.3. Opens keyword
opens is similar to open. If the open keyword is added to the module, the exports packages exported by default in the module are in the form of open.
module N { exports com.jdojo.claim.model; opens com.jdojo.claim.model;}3.4. Requires keywords
This keyword declares the dependency between the current module and another module. A bit similar to dependencies in maven.
require <module>;requires transitive <module>;requires static <module>;requires transitive static <module>;requires transitive static <module>;
Static modifiers can also be added to require statements, which means that the dependency is mandatory at compile time, but optional at runtime. The transitive modifier in the require statement will cause implicit dependencies for other modules dependent on the current module. Please see the following figure:
Here we can take a look at the module-info.class file under the java.se module:
/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //** * Defines the core Java SE API. * <P> * The modules defining the CORBA and Java EE APIs are not required by * this module, but they are required by the * <a href="java.se.ee-summary.html">{@code java.se.ee}</a> module. * * <dl> * <dt style="font-family:'DejaVu Sans', Arial, Helvetica, sans serif">Optional for the Java SE Platform:</dt> * <dd> * <a href="../specs/jni/index.html">Java Native Interface (JNI)</a><br> * <a href="../specs/jvmti.html">Java Virtual Machine Tool Interface (JVM TI)</a><br> * <a href="../specs/jdwp/jdwp-spec.html">Java Debug Wire Protocol (JDWP)</a><br> * </dd> * </dl> * * @moduleGraph * @since 9 */module java.se { requires transitive java.compiler; requires transitive java.datatransfer; requires transitive java.desktop; requires transitive java.instrument; requires transitive java.logging; requires transitive java.management; requires transitive java.management.rmi; requires transitive java.naming; requires transitive java.prefs; requires transitive java.rmi; requires transitive java.scripting; requires transitive java.security.jgss; requires transitive java.security.sasl; requires transitive java.sql; requires transitive java.sql.rowset; requires transitive java.xml; requires transitive java.xml.crypto;}At this time, we only need java.se, and we will indirectly introduce all dependencies under this module
3.5. Uses and provider keywords
Java allows the use of service provider mechanisms that separate service providers and service users. JDK 9 allows to implement its services using statements and providing statements. Use statements to specify the name of the service interface, and the current module will find it and load it using the java.util.ServiceLoader class. Please refer to the previous example for the code. Note: the classes provided by the provider must be under the same module, and the implementation of other modules cannot be referenced at present. For example: the StudentServiceImpl can only exist under the student.service module, and the interface implementation of the student.service module provider in other modules is not allowed.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.