java9学习之模块化

时间:2023-03-09 09:42:13
java9学习之模块化

  截止到目前JDK的版本已经更新到10了,虽然java9的生命周期才半年,但是我认为这个版本带来的变革是不可磨灭的,它是第一次深层次的针对架构以及依赖上的革新。下面我们就来学习一下。

一、模块化项目构建

  其实模块化本身不难理解,我们先前使用maven或者gradle就构建过多模块的项目。那么我们在java9里依然可以照猫画虎来构建一下我们的模块化项目工程。如图所示:

java9学习之模块化

注意以下几点:

  1.请在每个模块下创建一个叫做module-info.java的模块化描述文件

  2.在idea里配置一下模块依赖,在这里我们的project.portal模块如果依赖student.service模块,我们可以这么来设置:

  找到这个选项图标:java9学习之模块化,然后这样设置来添加依赖:

  java9学习之模块化

  如果需要设置其他项目的依赖项,也请按照此方式设置。

二、实现步骤

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();
}
}

我们运行后既可以拿到对应的结果:

java9学习之模块化

三、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修饰符会导致依赖于当前模块的其他模块具有隐式依赖性,请看下图:

java9学习之模块化

在这里我们可以看看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关键字

Java允许使用服务提供者和服务使用者分离的服务提供者机制。 JDK 9允许使用语句(uses statement)和提供语句(provides statement)实现其服务。使用语句可以指定服务接口的名字,当前模块就会发现它,使用 java.util.ServiceLoader类进行加载。代码请参考前面的例子,注意:provider提供的类必须在同一个模块下,当前不能引用其他模块的实现,比如说:前面的例子StudentServiceImpl只能存在student.service模块下,student.service模块provider其他的模块下的接口实现是不允许的。