摘要:在JAVA语言中,截止到目前JDK的版本已经更新到10了,虽然java9的生命周期才半年,但是我认为这个版本带来的变革是不可磨灭的,它是第一次深层次的针对架构以及依赖上的革新。下面我们就来学习一下。希望对大家学习JAVA语言有所帮助。
前言
在JAVA语言中,截止到目前JDK的版本已经更新到10了,虽然java9的生命周期才半年,但是我认为这个版本带来的变革是不可磨灭的,它是第一次深层次的针对架构以及依赖上的革新。下面我们就来学习一下。希望对大家学习JAVA语言有所帮助。
模块化的功能有几个目的:
让Java的SE程序更加容易轻量级部署
改进组件间的依赖管理,引入比Jar粒度更大的Module
改进性能和安全性
如果用更加简单解释,那就是"解决Classpath地狱问题,改进部署能力"。Module的内容比较多,为了由浅入深,我按照一些问题和我的理解来介绍模块化。
一、模块化项目构建
其实模块化本身不难理解,我们先前使用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) { ServiceLoaderstudentServices = ServiceLoader.load(IStudentService.class); studentServices.findFirst().get().study(); } }
复制代码
我们运行后既可以拿到对应的结果:
三、module-info.java文件常见配置
3.1、关于open关键字
open:该关键字如果加载模块上,那么通过exports的导出包下的类可见度是最高的,我们可以通过反射的方式来创建对对象和访问属性。
3.2、关于exports关键字
当我们定义好模块后,我们可以指定该模块下的哪些包可以被其他模块所访问,exports关键字就起到该作用。我们也可以配合to来指定哪些模块可以访问该包的内容
语法 exports 包名 [to] 模块名
exports
exports
复制代码
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
requires transitive
requires static
requires transitive static
复制代码
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. * * The modules defining the CORBA and Java EE APIs are not required by * this module, but they are required by the * {[url=home.php?mod=space&uid=24691]@code[/url] java.se.ee} module. * * * Optional for the Java SE Platform: * * Java Native Interface (JNI) * Java Virtual Machine Tool Interface (JVM TI) * Java Debug Wire Protocol (JDWP) * * * * @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其他的模块下的接口实现是不允许的。
希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!
擅长针对企业软件开发的产品设计及开发的细节与流程设计课程内容。座右铭:大道至简!
已有23人表明态度,87%喜欢该老师!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号