Java 9 模块

Java 9模块是Java结构中最大的变化之一。在这里,我将对“Java 9模块系统”进行简要的高层介绍。

Java 9 模块

我们将探讨关于Java 9模块系统的以下主题。

    介绍
    Java SE 9:拼图项目
    当前Java系统存在的问题?
    Java SE 9模块化系统的优势
    比较JDK 8和JDK 9
    什么是Java 9模块?
    Java 9模块系统的起源
    比较Java 8和Java 9应用程序

介绍

甲骨文公司将Java 9的发布日期从2017年3月推迟到2017年9月。我们知道,Java SE 8带来了三个重大新功能(以及其他一些增强和新功能)。

    Lambda表达式
    流(API)
    日期(API)

同样地,Java SE 9将配备三个重要功能(以及少数其他增强和新功能)。

    Java 模块系统(拼图项目)
    Java REPL
    整改项目硬币

在这篇文章中,我们将讨论Oracle Jigsaw项目的基础知识。

Java SE 9: 拼图项目

拼图项目将引入全新的Java SE 9概念:Java模块系统。这是甲骨文公司在Java SE 9版本发布中非常重大和有声望的项目。最初,他们将该项目作为Java SE 7版本的一部分开始。然而,由于巨大的变化,它被推迟到Java SE 8,然后又被推迟。现在,它即将在2017年9月与Java SE 9一起发布。拼图项目的主要目标是:

  • The Modular 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).- 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.- Modular Run-Time Images
    The main goal of this Feature is “Restructure the JDK and JRE run-time images to accommodate modules”.- Encapsulate Most Internal APIs
    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”.- Java Platform Module System
    The main goal of this Feature is “Allowing the user to create their modules to develop their applications”.- jlink: The Java Linker
    The main goal of this jlink Tool is “Allowing the user to create executable to their applications”.

如果你对这些事情不是很清楚,不要担心。我们将在接下来的部分和我即将发布的帖子中详细讨论这些概念,并提供一些有用的例子。

当前Java系统存在的问题是什么?

在本节中,我们将讨论“为什么我们需要Java SE 9模块系统”,这意味着现有Java系统存在的问题。开发或交付基于Java的应用程序时,Java SE 8或更早版本系统存在以下问题。

  • 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.
  • JAR files like rt.jar etc are too big to use in small devices and applications.
  • As JDK is too big, our applications or devices are not able to support better Performance.
  • There is no Strong Encapsulation in the current Java System because “public” access modifier is too open. Everyone can access it.
  • As JDK, JRE is too big, it is hard to Test and Maintain applications.
  • As the public is too open, They are not to avoid the accessing of some Internal Non-Critical APIs like sun.*, *.internal.* etc.
  • As User can access Internal APIs too, Security is also big issue.
  • Application is too big.
  • Its a bit tough to support Less Coupling between components.

为了解决所有这些问题,Oracle将在Java SE 9版本中发布Java模块化系统。

Java SE 9 模块系统的优势

Java SE 9 模块系统将带来以下好处。

  • 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.
  • 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.
  • 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).

我们将很快逐一探索所有这些概念。

比较Java开发工具包(JDK) 8与JDK 9。

jmods folder, java modules

Java 9模块是什么?

java 9 modules system

Java 9模块系统的创始人

截至目前,Java 9模块系统在早期访问JDK中有95个模块。Oracle公司已将JDK jars和Java SE规范分为两组模块。

  • All JDK Modules starts with “jdk.*”
  • All Java SE Specifications Modules starts with “java.*”

Java 9 模块系统有一个名为“java.base”的模块。它被称为基础模块。它是一个独立的模块,并且不依赖于任何其他模块。默认情况下,所有其他模块都依赖于这个模块。这就是为什么“java.base”模块也被称为 Java 9 模块的母模块。它是所有 JDK 模块和用户定义模块的默认模块。

比较Java 8和Java 9应用程序

java 9 application module system
  • One Module
  • Module Name
  • Module Descriptor
  • Set of Packages
  • Set of Types and Resources

这里的资源可以是module-info.java(模块描述符)或其他属性或XML。我们在这篇文章中讨论了很多理论。我认为已经足够理解Java 9模块系统的基础知识了。在我的下一篇文章中,我们将开始一些基本的模块开发。根据需要,我将讨论一些更多的理论概念或Java 9模块系统术语。这就是关于“Java 9模块系统介绍”的全部内容。在我的后续文章中,我们将讨论一些更多的Java SE 9模块系统概念。如果您喜欢我的文章或有任何问题/建议/输入错误,请给我留言。谢谢您阅读我的教程。愿您学习Java SE 9愉快!参考:Project Jigsaw