前言
截止到目前JDK的版本已經更新到10了,雖然java9的生命週期才半年,但是我認為這個版本帶來的變革是不可磨滅的,它是第一次深層次的針對架構以及依賴上的革新。下面我們就來學習一下。
模塊化的功能有幾個目的:
一、模塊化項目構建
其實模塊化本身不難理解,我們先前使用maven或者gradle就構建過多模塊的項目。那麼我們在java9裡依然可以照貓畫虎來構建一下我們的模塊化項目工程。如圖所示:
注意以下幾點:
1.請在每個模塊下創建一個叫做module-info.java的模塊化描述文件
2.在idea裡配置一下模塊依賴,在這裡我們的project.portal模塊如果依賴student.service模塊,我們可以這麼來設置:
找到這個選項圖標:,然後這樣設置來添加依賴:
如果需要設置其他項目的依賴項,也請按照此方式設置。
二、實現步驟
2.1、Student.Service模塊
2.1.1、編寫StudentService的module-info.java
示例代碼:
import com.bdqn.lyrk.student.service.SecondStudentService;import com.bdqn.lyrk.student.service.api.IStudentService;/** * 模塊化描述類,統一建立在各個模塊的源文件根目錄名字為:module-info.java * 模塊化常見的語法結構: * * import xxxx.xxxx; * .... * * [open] module 模塊名{ * requires [static|transitive] 模塊名; * exports 包名[to 模塊名] * providers 接口名with [接口實現類,....] * uses 接口名* * } * ** @author chen.nie* @date 2018/4/18**/module student.service { exports com.bdqn.lyrk.student.service.api; provides IStudentService with SecondStudentService;}2.1.2、定義接口
package com.bdqn.lyrk.student.service.api;public interface IStudentService { void study();}2.1.3、定義實現類
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 模塊
2.2.1、編寫module-info.java
import com.bdqn.lyrk.student.service.api.IStudentService;module project.portal { uses IStudentService; requires transitive student.service;}2.2.2、編寫Main方法
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(); }}我們運行後既可以拿到對應的結果:
三、module-info.java文件常見配置
3.1、關於open關鍵字
open:該關鍵字如果加載模塊上,那麼通過exports的導出包下的類可見度是最高的,我們可以通過反射的方式來創建對對象和訪問屬性。
3.2、關於exports關鍵字
當我們定義好模塊後,我們可以指定該模塊下的哪些包可以被其他模塊所訪問,exports關鍵字就起到該作用。我們也可以配合to來指定哪些模塊可以訪問該包的內容
語法exports 包名[to] 模塊名
exports <package>;exports <package> to <module1>, <module2>...;
3.3、opens關鍵字
opens類似於open,如果open關鍵字加在module上,那麼模塊裡默認導出的exports包都為open形式的
module N { exports com.jdojo.claim.model; opens com.jdojo.claim.model;}3.4、requires關鍵字
該關鍵字聲明當前模塊與另一個模塊的依賴關係。有點類似於maven中的dependecies。
requires <module>;requires transitive <module>;requires static <module>;requires transitive static <module>;
require語句中也可以加靜態修飾符,這樣的話表示在編譯時的依賴是強制的,但在運行時是可選的。 require語句中的transitive修飾符會導致依賴於當前模塊的其他模塊具有隱式依賴性,請看下圖:
在這裡我們可以看看java.se模塊下的module-info.class文件:
/* * 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;}此時我們只要requires java.se,那麼該模塊下的所有依賴我們就間接的引入了
3.5、uses與provider關鍵字
Java允許使用服務提供者和服務使用者分離的服務提供者機制。 JDK 9允許使用語句(uses statement)和提供語句(provides statement)實現其服務。使用語句可以指定服務接口的名字,當前模塊就會發現它,使用java.util.ServiceLoader類進行加載。代碼請參考前面的例子,注意:provider提供的類必須在同一個模塊下,當前不能引用其他模塊的實現,比如說:前面的例子StudentServiceImpl只能存在student.service模塊下,student.service模塊provider其他的模塊下的接口實現是不允許的。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。