java9學(xué)習(xí)筆記之模塊化詳解
前言
截止到目前JDK的版本已經(jīng)更新到10了,雖然java9的生命周期才半年,但是我認(rèn)為這個版本帶來的變革是不可磨滅的,它是第一次深層次的針對架構(gòu)以及依賴上的革新。下面我們就來學(xué)習(xí)一下。
模塊化的功能有幾個目的:
- 讓Java的SE程序更加容易輕量級部署
- 改進(jìn)組件間的依賴管理,引入比Jar粒度更大的Module
- 改進(jìn)性能和安全性
- 如果用更加簡單解釋,那就是"解決Classpath地獄問題,改進(jìn)部署能力"。Module的內(nèi)容比較多,為了由淺入深,我按照一些問題和我的理解來介紹模塊化。
一、模塊化項(xiàng)目構(gòu)建
其實(shí)模塊化本身不難理解,我們先前使用maven或者gradle就構(gòu)建過多模塊的項(xiàng)目。那么我們在java9里依然可以照貓畫虎來構(gòu)建一下我們的模塊化項(xiàng)目工程。如圖所示:

注意以下幾點(diǎn):
1.請?jiān)诿總€模塊下創(chuàng)建一個叫做module-info.java的模塊化描述文件
2.在idea里配置一下模塊依賴,在這里我們的project.portal模塊如果依賴student.service模塊,我們可以這么來設(shè)置:
找到這個選項(xiàng)圖標(biāo):,然后這樣設(shè)置來添加依賴:
如果需要設(shè)置其他項(xiàng)目的依賴項(xiàng),也請按照此方式設(shè)置。

二、實(shí)現(xiàn)步驟
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;
/**
* 模塊化描述類,統(tǒng)一建立在各個模塊的源文件根目錄 名字為:module-info.java
* 模塊化常見的語法結(jié)構(gòu):
*
* import xxxx.xxxx;
* ....
*
* [open] module 模塊名 {
* requires [static|transitive] 模塊名;
* exports 包名 [to 模塊名]
* providers 接口名 with [接口實(shí)現(xiàn)類,....]
* 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、定義實(shí)現(xiàn)類
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();
}
}
我們運(yùn)行后既可以拿到對應(yīng)的結(jié)果:

三、module-info.java文件常見配置
3.1、關(guān)于open關(guān)鍵字
open:該關(guān)鍵字如果加載模塊上,那么通過exports的導(dǎo)出包下的類可見度是最高的,我們可以通過反射的方式來創(chuàng)建對對象和訪問屬性。
3.2、關(guān)于exports關(guān)鍵字
當(dāng)我們定義好模塊后,我們可以指定該模塊下的哪些包可以被其他模塊所訪問,exports關(guān)鍵字就起到該作用。我們也可以配合to來指定哪些模塊可以訪問該包的內(nèi)容
語法 exports 包名 [to] 模塊名
exports <package>; exports <package> to <module1>, <module2>...;
3.3、opens關(guān)鍵字
opens類似于open,如果open關(guān)鍵字加在module上,那么模塊里默認(rèn)導(dǎo)出的exports包都為open形式的
module N {
exports com.jdojo.claim.model;
opens com.jdojo.claim.model;
}
3.4、requires關(guān)鍵字
該關(guān)鍵字聲明當(dāng)前模塊與另一個模塊的依賴關(guān)系。有點(diǎn)類似于maven中的dependecies。
requires <module>; requires transitive <module>; requires static <module>; requires transitive static <module>;
require語句中也可以加靜態(tài)修飾符,這樣的話表示在編譯時的依賴是強(qiáng)制的,但在運(yùn)行時是可選的。require語句中的transitive修飾符會導(dǎo)致依賴于當(dāng)前模塊的其他模塊具有隱式依賴性,請看下圖:
在這里我們可以看看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 class="simpleTagLabel" 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關(guān)鍵字
Java允許使用服務(wù)提供者和服務(wù)使用者分離的服務(wù)提供者機(jī)制。 JDK 9允許使用語句(uses statement)和提供語句(provides statement)實(shí)現(xiàn)其服務(wù)。使用語句可以指定服務(wù)接口的名字,當(dāng)前模塊就會發(fā)現(xiàn)它,使用 java.util.ServiceLoader類進(jìn)行加載。代碼請參考前面的例子,注意:provider提供的類必須在同一個模塊下,當(dāng)前不能引用其他模塊的實(shí)現(xiàn),比如說:前面的例子StudentServiceImpl只能存在student.service模塊下,student.service模塊provider其他的模塊下的接口實(shí)現(xiàn)是不允許的。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗(yàn)詳解
這篇文章主要介紹了Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗(yàn)詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng)
這篇文章主要介紹了關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng),匿名內(nèi)部類顧名思義是沒有修飾符甚至沒有名稱的內(nèi)部類,使用匿名內(nèi)部類需要注意哪些地方,我們一起來看看吧2023-03-03
Java將文件分割為多個子文件再將子文件合并成原始文件的示例
本篇文章主要介紹了Java將文件分割為多個子文件再將子文件合并成原始文件的示例,具有一定的參考價值,有興趣的可以了解一下。2017-02-02
java使用ZipInputStream實(shí)現(xiàn)讀取和寫入zip文件
zip文檔可以以壓縮格式存儲一個或多個文件,本文主要為大家詳細(xì)介紹了java如何使用ZipInputStream讀取Zip文檔與寫入,需要的小伙伴可以參考下2023-11-11
Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat
今天小編就為大家分享一篇關(guān)于Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Java解析zip文件,并識別壓縮包里面的文件轉(zhuǎn)換成可操作的IO流方式
這篇文章主要介紹了Java解析zip文件,并識別壓縮包里面的文件轉(zhuǎn)換成可操作的IO流方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
java發(fā)送http請求并獲取狀態(tài)碼的簡單實(shí)例
下面小編就為大家?guī)硪黄猨ava發(fā)送http請求并獲取狀態(tài)碼的簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05

