9模块化学习 java
Java 9 modules are one of the biggest change in the structure of java. Here I am going to give a brief high level introduction about “Java 9 Module System”.
Java 9模块是Java结构中最大的变化之一。 在这里,我将简要概述“ Java 9模块系统”。
Java 9模块 (Java 9 Modules)
We will look into following topics about java 9 modules system.
我们将研究有关Java 9模块系统的以下主题。
- Introduction 介绍
- Java SE 9: Jigsaw Project Java SE 9:拼图项目
- Problems of Current Java System? 当前的Java系统有问题吗?
- Advantages of Java SE 9 Module System Java SE 9模块系统的优点
- Compare JDK 8 and JDK 9 比较JDK 8和JDK 9
- What is Java 9 Module? 什么是Java 9模块?
- Mother of Java 9 Module System Java 9模块系统之母
- Compare Java 8 and Java 9 Applications 比较Java 8和Java 9应用程序
介绍 (Introduction)
Oracle corp has postponed Java 9 release from March 2017 to September 2017.
Oracle公司已将Java 9版本从2017年3月推迟到2017年9月 。
As we know, Java SE 8 came with three big new features (plus few more enhancements and new features).
众所周知,Java SE 8拥有三大新功能(以及一些其他增强功能和新功能)。
- Lambda Expressions Lambda表达式
- Stream API 流API
- Date API 日期API
In the same way, Java SE 9 is coming with three big features (plus few more enhancements and new features):
同样,Java SE 9具有三个主要功能(以及一些其他增强功能和新功能):
In this post, we will discuss Oracle Jigsaw project basics.
在本文中,我们将讨论Oracle Jigsaw项目的基础知识。
Java SE 9:拼图项目 (Java SE 9: Jigsaw Project)
Jigsaw project is going to introduce completely new concept of Java SE 9: Java Module System.
Jigsaw项目将引入Java SE 9的全新概念: Java模块系统 。
It is very big and prestigious project from Oracle Corp in Java SE 9 release. Initially, they have started this project as part of Java SE 7 Release. However, with huge changes, it’s postponed to Java SE 8 then again postponed. Now it is about to release with Java SE 9 in September 2017.
这是Oracle Corp在Java SE 9版本中非常重要的项目。 最初,他们作为Java SE 7 Release的一部分启动了该项目。 但是,由于进行了很大的更改,它被推迟到Java SE 8,然后又被推迟了。 现在它将与Java SE 9一起在2017年9月发布。
Main Goals of Jigsaw Project:
拼图项目的主要目标:
- The Modular JDK 模块化JDK
As we know, Current JDK system is too big. So they have decided to divide JDK itself into small modules to get a number of benefits (We will discuss them soon in the coming sections).
众所周知,当前的JDK系统太大。 因此,他们决定将JDK本身划分为小模块,以获得许多好处(我们将在接下来的部分中很快讨论它们)。
- Modular Source Code 模块化源代码
Current source code jar files are too big, especially rt.jar is too big right. So they are going to divide Java Source code into smaller modules.
当前的源代码jar文件太大,尤其是rt.jar太大了。 因此,他们打算将Java源代码分成较小的模块。
- Modular Run-Time Images 模块化运行时映像
The main goal of this Feature is “Restructure the JDK and JRE run-time images to accommodate modules”.
此功能的主要目标是“重组JDK和JRE运行时映像以容纳模块”。
- Encapsulate Most Internal APIs 封装大多数内部API
The main goal of this feature is “Make most of the JDK’s internal APIs inaccessible by default but leave a few critical, widely-used internal APIs accessible”.
此功能的主要目标是“使默认情况下无法访问大多数JDK的内部API,但保留一些关键的,广泛使用的内部API”。
- Java Platform Module System Java平台模块系统
The main goal of this Feature is “Allowing the user to create their modules to develop their applications”.
此功能的主要目标是“允许用户创建其模块以开发其应用程序”。
- jlink: The Java Linker jlink:Java链接器
The main goal of this jlink Tool is “Allowing the user to create executable to their applications”.
该jlink工具的主要目标是“允许用户创建其应用程序的可执行文件”。
If you don’t understand these things clearly, do NOT worry. We will discuss these concepts in-depth with some useful examples in the coming sections and also in my coming posts.
如果您不清楚这些事情,请不要担心。 在接下来的部分以及我的后续文章中,我们将通过一些有用的示例深入讨论这些概念。
当前的Java系统有问题吗? (Problems of Current Java System?)
In this section, we will discuss “Why we need Java SE 9 Module System” that means the problems of Current Java System.
在本节中,我们将讨论“为什么需要Java SE 9模块系统”,这意味着当前的Java系统存在问题。
Java SE 8 or earlier systems have following problems in developing or delivering Java Based applications.
Java SE 8或更早的系统在开发或交付基于Java的应用程序时存在以下问题。
- As JDK is too big, it is a bit tough to scale down to small devices. Java SE 8 has introduced 3 types of compact profiles to solve this problem: compact1, compact2, and compact3. But it does not solve this problem. 由于JDK太大,因此很难缩小到小型设备。 Java SE 8引入了三种类型的压缩概要文件来解决此问题:compact1,compact2和compact3。 但是它不能解决这个问题。
- JAR files like rt.jar etc are too big to use in small devices and applications. rt.jar等JAR文件太大,无法在小型设备和应用程序中使用。
- As JDK is too big, our applications or devices are not able to support better Performance. 由于JDK太大,我们的应用程序或设备无法支持更好的性能。
- There is no Strong Encapsulation in the current Java System because “public” access modifier is too open. Everyone can access it. 当前的Java系统中没有强封装,因为“公共”访问修饰符太开放了。 每个人都可以访问它。
- As JDK, JRE is too big, it is hard to Test and Maintain applications. 由于JDK,JRE太大,因此很难测试和维护应用程序。
- As the public is too open, They are not to avoid the accessing of some Internal Non-Critical APIs like sun.*, *.internal.* etc. 由于公众过于开放,他们不应该避免访问某些内部非关键API,例如sun。*,*。internal。*等。
- As User can access Internal APIs too, Security is also big issue. 由于用户也可以访问内部API,因此安全性也是一个大问题。
- Application is too big. 应用程序太大。
- Its a bit tough to support Less Coupling between components. 支持组件之间的较少耦合有点困难。
To solve all these problems, Oracle Corp is going to release Java Module system in Java SE 9 Release.
为了解决所有这些问题,Oracle Corp将在Java SE 9 Release中发布Java Module系统。
Java SE 9模块系统的优点 (Advantages of Java SE 9 Module System)
Java SE 9 Module System is going to provide the following benefits
Java SE 9 Module System将提供以下好处
- As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever modules we want. So it is very easy to scale down the Java Application to Small devices. 由于Java SE 9将把JDK,JRE,JAR等分成较小的模块,因此我们可以使用所需的任何模块。 因此,将Java应用程序缩减为小型设备非常容易。
- Ease of Testing and Maintainability. 易于测试和可维护性。
- Supports better Performance. 支持更好的性能。
- As public is not just public, it supports very Strong Encapsulation. (Don’t worry its a big concept. we will explore it with some useful examples soon). 公共不仅是公共的,它还支持非常强的封装。 (不要担心它是一个大概念。我们将很快通过一些有用的示例对其进行探讨)。
- We cannot access Internal Non-Critical APIs anymore. 我们无法再访问内部非关键API。
- Modules can hide unwanted and internal details very safely, we can get better Security. 模块可以非常安全地隐藏不必要的内部细节,我们可以获得更好的安全性。
- Application is too small because we can use only what ever modules we want. 应用程序太小,因为我们只能使用所需的模块。
- Its easy to support Less Coupling between components. 它易于支持组件之间的较少耦合。
- Its easy to support Single Responsibility Principle (SRP). 它易于支持单一职责原则(SRP)。
We will explore all these concepts one by one soon.
我们将很快一一探讨所有这些概念。
比较JDK 8和JDK 9 (Compare JDK 8 and JDK 9)
We know what a JDK software contains. After installing JDK 8 software, we can see a couple of directories like bin, jre, lib etc in Java Home folder.
我们知道JDK软件包含的内容。 安装JDK 8软件后,我们可以在Java Home文件夹中看到几个目录,例如bin,jre,lib等。
However, Oracle Corp has changed this folder structure a bit differently as shown below.
但是,Oracle Corp对该文件夹结构的更改有些不同,如下所示。
JDK 8 Folder Structure:
JDK 8文件夹结构:
JDK 9 Folder Structure:
JDK 9文件夹结构:
Here JDK 9 does NOT contain JRE. In JDK 9, JRE is separated into a separate distribution folder. JDK 9 software contains a new folder “jmods”. It contains a set of Java 9 Modules as shown below.
这里的JDK 9不包含JRE。 在JDK 9中,JRE分为一个单独的分发文件夹。 JDK 9软件包含一个新文件夹“ jmods”。 它包含一组Java 9模块,如下所示。
In JDK 9, No rt.jar and No tools.jar
在JDK 9中,没有rt.jar和没有tools.jar
NOTE:-
As of today, “jmods” contains 95 modules. It may increase in the final release.
“jmods” folder is available at ${JAVA_HOME}/jmods. These are known as JDK Modules.
注意:-
到目前为止,“ jmods”包含95个模块。 在最终版本中可能会增加。
$ {JAVA_HOME} / jmods中有“ jmods”文件夹。 这些被称为JDK模块。
什么是Java 9模块? (What is Java 9 Module?)
A Module is a self-describing collection of Code, Data, and some Resources. It is a set of related Packages, Types (classes, abstract classes, interfaces etc) with Code & Data and Resources.
模块是代码,数据和一些资源的自描述集合。 它是一组与代码,数据和资源相关的包,类型(类,抽象类,接口等)。
Each Module contains only a set of related code and data to support Single Responsibility (Functionality) Principle (SRP).
每个模块仅包含一组相关的代码和数据,以支持单一职责(功能)原则(SRP)。
The main goal of Java 9 Module System is to support Modular Programming in Java.
Java 9模块系统的主要目标是支持Java 模块化编程 。
We will discuss on “What is a Module Descriptor” and “How to develop Java Modules” in my coming posts.
我们将在我的后续文章中讨论“什么是模块描述符”和“如何开发Java模块”。
Java 9模块系统之母 (Mother of Java 9 Module System)
As of now, Java 9 Module System has 95 modules in Early Access JDK. Oracle Corp has separated JDK jars and Java SE Specifications into two set of Modules.
到目前为止,Java 9 Module System在Early Access JDK中具有95个模块。 Oracle Corp已将JDK jar和Java SE规范分为两组模块。
- All JDK Modules starts with “jdk.*” 所有JDK模块均以“ jdk。*”开头
- All Java SE Specifications Modules starts with “java.*” 所有Java SE规范模块均以“ java。*”开头
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module and does NOT dependent on any other modules. By default, all other Modules dependent on this module.
Java 9模块系统具有一个“ java.base”模块 。 它被称为基本模块。 这是一个独立模块,不依赖于其他模块。 默认情况下,所有其他模块都依赖于此模块。
That’s why “java.base” Module is also known as The Mother of Java 9 Modules.
这就是“ java.base”模块也被称为Java 9模块之母的原因。
It’s default module for all JDK Modules and User-Defined Modules.
它是所有JDK模块和用户定义模块的默认模块。
比较Java 8和Java 9应用程序 (Compare Java 8 and Java 9 Applications)
We have already developed many Java applications using Java 5, 6,7, or 8. We know how a Java 8 or earlier applications looks like and what it contains.
我们已经使用Java 5、6、7或8开发了许多Java应用程序。我们知道Java 8或更早版本的应用程序的外观及其包含的内容。
In brief, I have depicted a Java 8 applications in a diagram as shown below:
简而言之,我在下图中描述了Java 8应用程序:
In a Java 8 or earlier applications, Top level component a Package. It groups a set related to types into a group. It also contains a set of resources.
在Java 8或更早版本的应用程序中,顶级组件是Package。 它将与类型相关的集合归为一组。 它还包含一组资源。
Java 9 Applications does not have much difference with this. It just introduced a new component called “Module”, which is used to group a set of related Packages into a group. And one more new component that Module Descriptor (“module-info.java”). That’s it.
Java 9应用程序与此没有太大区别。 它刚刚引入了一个称为“模块”的新组件,该组件用于将一组相关的软件包分组为一组。 还有一个模块描述符(“ module-info.java”)的新组件。 而已。
Rest of the application is same as earlier versions of applications as shown below.
该应用程序的其余部分与早期版本的应用程序相同,如下所示。
Like Java 8 applications have Packages as a Top level components, Java 9 applications have Module as Top Level components.
像Java 8应用程序将Packages作为顶级组件一样,Java 9应用程序将Module作为顶级组件。
NOTE:-
Each Java 9 Module have one and only one Module and one Module Descriptor. Unlike Java 8 Packages, We cannot create multiple modules into a single Module.
注意:-
每个Java 9模块只有一个模块和一个模块描述符。 与Java 8包不同,我们不能在一个模块中创建多个模块。
In brief I can say a Java 9 Module contains the following main components:
简而言之,我可以说Java 9模块包含以下主要组件:
- One Module 一个模块
- Module Name 模块名称
- Module Descriptor 模块描述符
- Set of Packages 套套
- Set of Types and Resources 类型和资源集
Here Resources may be module-info.java (Module Descriptor) or any other properties or XML.
这里的资源可以是module-info.java(模块描述符)或任何其他属性或XML。
We have discussed too much theory in this post. I think that is enough to understand the basics of Java 9 Module system. We will start some basic Module development in my coming post. I will discuss some more Theory concepts or Java 9 Module System terminology as and when required.
我们在这篇文章中讨论了太多的理论。 我认为足以理解Java 9 Module系统的基础知识。 在我的后续文章中,我们将开始一些基本的Module开发。 在需要时,我将讨论更多的Theory概念或Java 9 Module System术语。
That’s it all about “Introduction to Java 9 Module System” topic. We will discuss some more Java SE 9 Module System concepts in my coming posts.
这就是“ Java 9模块系统简介”主题的全部内容。 在我的后续文章中,我们将讨论更多Java SE 9 Module System概念。
Please drop me a comment if you like my post or have any issues/suggestions/type errors.
如果您喜欢我的帖子或有任何问题/建议/类型错误,请给我评论。
Thank you for reading my tutorials.
感谢您阅读我的教程。
Happy Java SE 9 Learning!
Java SE 9学习愉快!
Reference: Project Jigsaw
参考: 拼图项目
9模块化学习 java