npm安装模块版本符_Java SE 9:模块和模块描述符基础知识(第2部分)

npm安装模块版本符

I have already discuss about “Java 9 Module System” in high level in my previous post. I’m going to discuss about two more important concepts in this post: “Module” and “Module Descriptor” basics , which are required to start Java 9 Modules Development.

在上一篇文章中,我已经在较高级别上讨论了“ Java 9模块系统”。 我将在本文中讨论两个更重要的概念:“模块”和“模块描述符”基础知识,这是开始Java 9模块开发所必需的。

In this series of “Java Module System” posts, it is my second post. Before reading this post, please read my first post by clicking here “Introduction to Java 9 Module System” to understand about Jigsaw Project details.

在“ Java模块系统”系列文章中,这是我的第二篇文章。 在阅读本文之前,请通过单击此处“ Java 9 Module System简介 ”阅读我的第一篇文章,以了解Jigsaw Project的详细信息。

Let us dive into this post now.

现在让我们进入这篇文章。

发表简要目录: (Post Brief Table of Content:)

  • Introduction to Java 9 Module

    Java 9模块简介
  • Java 9 Module Basics

    Java 9模块基础
  • Java 9 Module Rules

    Java 9模块规则
  • Java 9 Module Descriptor

    Java 9模块描述符
  • What is Module Meta Data?

    什么是模块元数据?
  • Java 9 Module Descriptor Syntax

    Java 9模块描述符语法
  • Are “module”, “requires”, and “exports” Keywords?

    是“模块”,“需要”和“导出”关键字吗?
  • Java 8 Jars Vs Java 9 Modules

    Java 8 Jars与Java 9模块
  • ClassPath Vs ModulePath

    类路径与模块路径
  • What is Modular JAR?

    什么是模块化JAR?

Java 9模块简介 (Introduction to Java 9 Module)

A Java 9 Module is a named, self-describing collection of Code and Data. It’s Code is organized as a set of Packages containing types (classes, abstract classes, interfaces, enums etc). It’s Data includes resources and other kinds of static information.

Java 9模块是一个命名的,自描述的代码和数据集合。 它的代码组织为一组包含类型(类,抽象类,接口,枚举等)的Package。 它的数据包括资源和其他种类的静态信息。

In Simple Terms,

  A Module = Code + Data

Here Even though both Data and Resources are same, in few diagrams I have depicted them in separated boxes for clarity purpose.

尽管数据和资源都是相同的,但为清晰起见,在少数几个图中,我在单独的方框中进行了描述。

NOTE:-
Module Descriptor (“module-info.java”) is a one of the Resources in Java 9 Module.

注意:-
模块描述符(“ module-info.java”)是Java 9模块中的资源之一。

The two main goals of Java 9 Module System:

Java 9模块系统的两个主要目标:

  • Reliable Configuration

    可靠的配置
  • Strong Encapsulation

    强封装

As we are beginners to the Java 9 Module System, it’s really tough to understand these two terms. No worries, we will discuss these two terms in-depth after developing couple of Modules in my coming posts.

因为我们是Java 9 Module System的初学者,所以很难理解这两个术语。 不用担心,在我的后续文章中开发了几个模块后,我们将深入讨论这两个术语。

In Java 9 Module System, a Module is a First class citizen.

在Java 9模块系统中,模块是头等公民。

In Simple Terms, Java has these many First class citizens:

  In OOP (Object Oriented Programming)  = Object 
  In FP (Functional Programming)        = Function or Lambda
  In Module System (Modular Programming)= Module

Java 9模块基础 (Java 9 Module Basics)

We should remember the following important points about Java 9 Module:

我们应该记住有关Java 9 Module的以下要点:

  • Each module has a unique Name.

    每个模块都有一个唯一的名称。
  • Each module has some description in a source file.

    每个模块在源文件中都有一些描述。
  • A Module description is expressed in a source file called “module-info.java”.

    模块描述在称为“ module-info.java”的源文件中表达。
  • As “module-info.java” file describes a Module, it is also known as “Module Descriptor”.

    由于“ module-info.java”文件描述了一个模块,因此也称为“模块描述符”。
  • A Module Descriptor is a Java file. It is not an XML, Text file or Properties file.

    模块描述符是Java文件。 它不是XML,文本文件或属性文件。
  • By convention, we should use same name “module-info.java” for Module Descriptor.

    按照约定,对于模块描述符,应使用相同的名称“ module-info.java”。
  • By convention, Module Descriptor file is placed in the top level directory of a Module.

    按照约定,模块描述符文件位于模块的顶级目录中。
  • Each Module can have any number of Packages and Types.

    每个模块可以具有任意数量的包和类型。
  • As of now, JDK 9 EA (Early Access) have 95 modules.

    到目前为止,JDK 9 EA(早期访问)具有95个模块。
  • We can create our own modules.

    我们可以创建自己的模块。
  • One Module can dependent on any number of modules.

    一个模块可以依赖于任意数量的模块。
  • Each Module should have one and only one Module Descriptor (“module-info.java”).

    每个模块应该只有一个模块描述符(“ module-info.java”)。

Using Java SE 9, we develop Modules that means we can do Modular Programming in Java.

使用Java SE 9,我们开发了模块,这意味着我们可以用Java进行模块化编程。

Java 9模块规则 (Java 9 Module Rules)

We should follow these rules to develop Java 9 Modules:

我们应该遵循以下规则来开发Java 9模块:

  • Each module have a unique name

    每个模块都有一个唯一的名称
  • As Modules live in a global space in JVM, each module should have a unique name. Like package names and JAR file names, we can use Reverse Domain Name pattern to define a module name.

    由于模块生活在JVM的全局空间中,因此每个模块应具有唯一的名称。 像包名和JAR文件名一样,我们可以使用“反向域名”模式来定义模块名。

    For example:-
    We are going to develop modules for www.journaldev.com, then you can use “com.journaldev.mod1” as your first module name, then “com.journaldev.mod2” and so on.

    例如:-
    我们将为www.journaldev.com开发模块,然后您可以使用“ com.journaldev.mod1”作为第一个模块名称,然后使用“ com.journaldev.mod2”,依此类推。

  • Each module should have one and only one Module Descriptor

    每个模块应该只有一个模块描述符
  • By convention, Java 9 Module system allows “module-info.java” as Module Descriptor. It should be placed at root folder of the Module.

    按照约定,Java 9模块系统允许“ module-info.java”作为模块描述符。 应将其放置在模块的根文件夹中。

    For example:-
    If We are going to develop “com.journaldev.mod1” module, then we should place our Module Descriptor under “com.journaldev.mod1” name.

    例如:-
    如果要开发“ com.journaldev.mod1”模块,则应将模块描述符放置在“ com.journaldev.mod1”名称下。

  • A Module can have any number of packages.

    一个模块可以具有任意数量的软件包。
  • One Module cannot have multiple sub-module code and description.

    一个模块不能具有多个子模块代码和描述。

There are few more import Rules in Java 9 Module System. We should discuss them in my coming posts soon.

Java 9模块系统中的导入规则很少。 我们应该在以后的文章中讨论它们。

Java 9模块描述符 (Java 9 Module Descriptor)

In a Java 9 Module, Module Descriptor is a resource, which contains Module Meta Data. It is NOT an XML or a properties file, a plain Java file.

在Java 9模块中,模块描述符是一种资源,其中包含模块元数据 。 它不是XML或属性文件,不是纯Java文件。

We must name this file as “module-info.java”. By convention, we mush place it at the root folder of the module. It is used to provide Module Description.

我们必须将此文件命名为“ module-info.java”。 按照约定,我们将其放置在模块的根文件夹中。 它用于提供模块描述。

Like other Java source files, a Module file is compiled into “module-info.class” using javac command. We will see it in my next post.

像其他Java源文件一样,使用javac命令将Module文件编译为“ module-info.class”。 我们将在我的下一篇文章中看到它。

A Module Descriptor is created using “module” keyword as shown below:

使用“ module”关键字创建一个模块描述符,如下所示:

module  {
  // Module Meta Data goes here.
}

Example:-

例:-

module com.journaldev {
 
}

It is a simple and minimal Module Descriptor example. Now Let us discuss what is a Module Meta Data?

这是一个简单而最少的模块描述符示例。 现在让我们讨论什么是模块元数据

什么是模块元数据? (What is Module Meta Data?)

As we discussed in the previous section, we can use Module Descriptor to provide Module Meta Data. A Module has the following Meta Data:

如前一节所述,我们可以使用模块描述符来提供模块元数据。 一个模块具有以下元数据:

  • A unique name.

    唯一的名称。
  • exports clause.

    出口条款。
  • requires clause.

    require子句。

Let us discuss these points in-depth with some useful examples here.

让我们在这里用一些有用的例子深入讨论这些要点。

  • Module has a unique name.

    模块具有唯一的名称。
  • We use “module” keyword to define module name as shown below:
    Example:-

    我们使用“模块”关键字来定义模块名称,如下所示:
    例:-

module com.journaldev {
 
}
  • A Module can exports it’s packages to outside world so that other Modules can use them.

    一个模块可以将其软件包导出到外界,以便其他模块可以使用它们。
  • In Module Descriptor, we use “exports” clause to export a package(s) to outside world or to other Modules.

    在模块描述符中,我们使用“ exports”子句将包导出到外界或其他模块。

    Example:-

    例:-

    module com.journaldev.mod1 {
       exports com.journaldev.services;
    }

    NOTE:-
    It is not mandatory to export all packages. It’s up-to the Module Owner to decide which one to export and which one not.

    注意:-
    并非必须导出所有软件包。 由模块负责人决定要导出哪个,不导出。

  • A Module can import or use other modules packages.

    一个模块可以导入或使用其他模块软件包。
  • In Module Descriptor, we use “requires” clause to import other Module’s packages.

    在模块描述符中,我们使用“ requires”子句导入其他模块的软件包。

    Example:-

    例:-

    module com.journaldev.mod2 {
       requires com.journaldev.services;
    }

    In the previous point, “com.journaldev.mod1” has exported “com.journaldev.services” package so that here “com.journaldev.mod2” is importing and using them.

    在上一点中,“ com.journaldev.mod1”已导出“ com.journaldev.services”包,因此此处“ com.journaldev.mod2”正在导入和使用它们。

    A Module can have more than this Meta Data. As a Beginner to Java 9 Module System, it is enough to start Modular Programming. We will discuss those few more Meta Data in my coming posts.

    一个模块可以具有更多的元数据。 作为Java 9模块系统的初学者,足以启动模块化编程。 我们将在我的后续文章中讨论更多的元数据。

    We will discuss a Module Descriptor Syntax in the next section.

    我们将在下一节讨论模块描述符语法。

    Java 9模块描述符语法 (Java 9 Module Descriptor Syntax)

    In brief, a Module Descriptor contains 3 main things:

    简而言之,模块描述符包含3个主要内容:

    • A Module name.

      模块名称。
    • exports clause.

      出口条款。
    • requires clause.

      require子句。

    Important points to remember about a Module Descriptor

    关于模块描述符要记住的重要点

    • A Module Descriptor just have a Module name and nothing that means no exports clause and no requires clause.

      模块描述符只有一个模块名称,什么也没有,这意味着没有exports子句和requires子句。
    • A Module Descriptor can have only exports clause, no requires clause. That means it is exports it’s packages to other Modules and NOT depending on any other modules. It’s an Independent module.

      模块描述符只能具有export子句,而不能具有require子句。 这意味着它将包导出到其他模块,而不依赖于任何其他模块。 这是一个独立的模块。
    • A Module Descriptor can have both exports clause and requires clause. That means it is exports it’s packages to other Modules and also using other Module’s packages. It is depending on any other modules. It’s NOT an Independent module.

      模块描述符可以同时具有exports子句和requires子句。 这意味着它会将其程序包导出到其他模块,并使用其他模块的程序包。 这取决于任何其他模块。 它不是独立模块。
    • A Module Descriptor can have zero, one or more exports clause.

      模块描述符可以具有零个,一个或多个export子句。
    • A Module Descriptor can have zero, one or more requires clause.

      一个模块描述符可以有零个,一个或多个require子句。

    NOTE:- Here “requires” means importing a package from Other module into the current Module.

    注意:-这里的“需要”是指将软件包从“其他”模块导入当前模块。

    In Simple Terms,
    
     "requires" = "imports"

    Example:-

    例:-

    module com.hello{
       exports  com.hello;
       requires java.sql;
    }

    Here “com.hello” Module is exporting it’s package “com.hello” to other Modules and importing or using “java.sql” Java 9 Module.

    在这里,“ com.hello”模块将其包“ com.hello”导出到其他模块,并导入或使用“ java.sql” Java 9模块。

    是“模块”,“需要”和“导出”关键字吗? (Are “module”, “requires”, and “exports” Keywords?)

    As we know in Java 9 Module System, We can use “module”, “requires”, and “exports” keywords to describe a Module Meta Data in a Module Descriptor.

    正如我们在Java 9 Module System中所知道的,我们可以使用“ module”,“ requires”和“ exports”关键字在模块描述符中描述模块元数据。

    However, all these 3 are not keywords. They are just “Contextual Keywords” only. That means they are keywords only with a Module Descriptor (“module-info.java”) file.

    但是,所有这3个都不是关键字。 它们仅是“ 上下文关键字 ”。 这意味着它们仅是带有模块描述符(“ module-info.java”)文件的关键字。

    If we used them as Identifiers in the existing code base, Java Compiler or Run-time does NOT throw any error or Exceptions at compile-time and run-time.

    如果我们在现有代码库中将它们用作标识符,则Java Compiler或Run-time在编译时和运行时不会引发任何错误或异常。

    We can use them as Identifiers without any issues as shown below:

    我们可以将它们用作标识符,而不会出现任何问题,如下所示:

    jshell> int module = 10
    module ==> 10
    
    jshell> String requires = "Hello World"
    requires ==> "Hello World"
    
    jshell> String exports = "Rams"
    exports ==> "Rams"

    Java 8 Jars与Java 9模块 (Java 8 Jars Vs Java 9 Modules)

    As we know in Java 8 or earlier versions, we use create JARs (Of course WARS or EARs) to deploy them into servers or use them in other applications.

    如我们在Java 8或更早版本中所知,我们使用创建JAR(当然是WARS或EAR)将它们部署到服务器中或在其他应用程序中使用。

    When we create Java 8 or earlier versions project in Eclipse IDE or any IDE, IDE adds lots of JDK JARs into our Project CLASSPATH as shown below:

    当我们在Eclipse IDE或任何IDE中创建Java 8或更早版本的项目时,IDE会向我们的Project CLASSPATH中添加许多JDK JAR,如下所示:

    When we create Java 9 Module project in Eclipse IDE or any IDE, IDE adds lots of JDK Modules into our Project MODULEPATH as shown below:

    当我们在Eclipse IDE或任何IDE中创建Java 9 Module项目时,IDE会向我们的Project MODULEPATH中添加许多JDK Module,如下所示:

    From Java 9 on-wards, We refer MODULEPATH instead of CLASSPATH. Of course, Java 9 supports both MODULEPATH and CLASSPATH.

    从Java 9开始,我们引用MODULEPATH而不是CLASSPATH。 当然,Java 9支持MODULEPATH和CLASSPATH。

    We will see about “When we use MODULEPATH?” and “When we use CLASSPATH?” in my coming posts in depth.

    我们将看到“何时使用MODULEPATH?” 和“何时使用CLASSPATH?” 在我接下来的帖子中有更深入的介绍。

    In Java 9, we can develop both Public API and Private API in a Module as shown below.

    在Java 9中,我们可以在一个模块中开发Public API和Private API,如下所示。

    类路径与模块路径 (ClassPath Vs ModulePath)

    As a Java Developer, we know what is CLASSPATH right. So far we are in CLASSPATH Hell or JAR Hell. From Java 9 on-wards, we are going to jump into another Hell: That is MODULEPATH Hell.

    作为Java开发人员,我们知道CLASSPATH是正确的。 到目前为止,我们处于CLASSPATH Hell或JAR Hell中。 从Java 9开始,我们将跳到另一个地狱:即MODULEPATH地狱。

    A ClassPath is a sequence of classes and packages (or JARs) which are user-defined classes and built-in classes. JVM or Java Compiler requires this to compile the application or classes.

    ClassPath是一系列类和包(或JAR),它们是用户定义的类和内置类。 JVM或Java编译器要求使用它来编译应用程序或类。

    A ModulePath is a sequence of Modules (which are provided in a Folder format or JAR format). If a Module is in folder format, that means its in Exploded Module format. If its in a JAR format, that jar is know as “Modular JAR“.

    ModulePath是一系列模块(以Folder格式或JAR格式提供)。 如果模块为文件夹格式,则表示其为爆炸模块格式。 如果其为JAR格式,则该jar称为“ 模块化JAR ”。

    Don’t worry if you don’t understand these concepts. As we are going to learn new concept, it takes time to understand the new terminology. Move on to the next post.

    如果您不了解这些概念,请不要担心。 在学习新概念时,需要花费一些时间来理解新术语。 继续下一篇文章。

    I will explain all these concepts in-detail in my coming posts. ModulePath is one of the new concept or terminology in Java 9 Module System. We should understand it in-detail.

    我将在以后的文章中详细解释所有这些概念。 ModulePath是Java 9 Module System中的新概念或术语之一。 我们应该详细了解它。

    That’s it all about “Java 9 Module Basics” topic. We will start Java SE 9 Simple Modules Development in my coming posts.

    这就是“ Java 9模块基础知识”主题的全部内容。 在我的后续文章中,我们将开始Java SE 9简单模块开发。

    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学习愉快!

    翻译自: https://www.journaldev.com/13596/javase9-module-basics-part2

    npm安装模块版本符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值