The eighth part of the Java9 series-Module modular programming

I plan to write a series of articles about Java 9 in the next period of time. Although Java 9 is not a core Java version like Java 8 or Java 11, there are still many features worth paying attention to. Looking forward to your attention, I will write a series of articles about java 9, about ten articles, this article is the eighth article.

In the Java 9 version, the Java language introduced a very important concept: module. If you are familiar with JavaScript code modular management, you should feel familiar with Java 9's modular management.

1. What is a Java module?

Similar to package in Java, module introduces another level of Java code grouping. Each such grouping (module) contains many sub-packages. Declare the folder and its subfolders as a module by adding the file module-info.java to the root of the source code file package of a module. The file syntax is as follows:

 module xxx.yyy{
  ....
 }

Where xxx.yyy is the declared name of the module module, not the package name.

Two, module export package

The file module-info.java can specify which packages under the module are visible and accessible to the outside world. exportsThis function is achieved through a new keyword .

 module xxx.yyy{
  exports com.zimug.java9;
 }

com.zimug.java9Represents a package.

It should be noted that even if the classes in a given package are public, if their packages are not explicitly exported through'exports', they will not be visible outside the module (both at compile time and runtime) .

Three, module import package

If another module wants to use the classes in the exported package, it can requiresimport (read) the package package of the target module with keywords in its module-info.java file.

module def.stu{
 requires xxx.yyy;
}

Fourth, the meaning of Java module

In my opinion, Java 9 introduces the module modular management system, which is more from the perspective of security. More than 90% of vulnerabilities in Java code are caused by reflection and insufficient granularity of access control. Java 9's modular system can just solve this problem. The Java 9 module provides another level of control over the visibility and accessibility of Java code.

For example: we all know that when a class is modified as private, it means that the class is an inner class. For top-level classes (external classes), there are only two modifiers: public and default. This also means a problem. Some public classes were originally intended to be used within the scope of the jar package definition, but the result is that any project that introduces this jar can use all the public class codes in this jar.

That is to say, our original intention was to provide public access within a limited scope, but the result was unrestricted public access. After the introduction of modular Java 9 may be implemented code public access to a limited extent , the code public divided into: open access to a limited range of external modules and publicly accessible inside the module .

Five, examples

In this example, I will create two modules "common.widget" and "data.widget" and place them under a single folder "modules-examples/src". The file "module-info.java" will be placed in the root folder of each module. The file and directory format is as follows:

D:\modules-example>tree /F /A
\---src
    +---common.widget
    |   |   module-info.java
    |   |   
    |   +---com
    |   |   \---zimug
    |   |           RendererSupport.java
    |   |           
    |   \---org
    |       \---jwidgets
    |               SimpleRenderer.java
    |               
    \---data.widget
        |   module-info.java
        |   
        \---com
            \---example
                    Component.java

The first module

This code file directory: modules-example/src/common.widget/org/jwidgets/SimpleRenderer.java. This package is not exported in the following text.

package org.jwidgets;

public class SimpleRenderer {
  public void renderAsString(Object object) {
      System.out.println(object);
  }
}

This code file directory: modules-example/src/common.widget/com/zimug/RendererSupport.java. This package will be exported later.

package com.zimug;

import org.jwidgets.SimpleRenderer;

public class RendererSupport {
  public void render(Object object) {
      new SimpleRenderer().renderAsString(object);
  }
}

Module export, this code file directory: modules-example/src/common.widget/module-info.java. Only export com.zimugpackage, no export org.jwidgetspackage. The exported module name iscommon.widget

module common.widget{
  exports com.zimug;
}

Second module

Module import common.widget, this code file directory: modules-example/src/data.widget/module-info.java

module data.widget {
  requires common.widget;
}

Use common.widgetpackage: in the imported module com.zimug. This code file path: modules-example/src/data.widget/com/example/Component.java

package com.example;

import com.zimug.RendererSupport;

public class Component {
  public static void main(String[] args) {
      RendererSupport support = new RendererSupport();
      support.render("Test Object");
  }
}

Compile and execute normally, and the results are as follows:

Test Object

Try to use package code that is not exported

Since the package "org.jwidgets" has not been exported through the "common.widget" module, another module "data.widget" cannot use the classes under the package package SimpleRenderer. Let's make a counter example and see what happens:

package com.example;
import org.jwidgets.SimpleRenderer;

public class Component {
  public static void main(String[] args) {
    SimpleRenderer simpleRenderer = new SimpleRenderer(); 
    simpleRenderer.renderAsString("Test Object");
  }
}

The compilation error message is as follows:

D:\modules-example\src\data.widget\com\example\Component.java:3: error: package org.jwidgets is not visible
import org.jwidgets.SimpleRenderer;
          ^
  (package org.jwidgets is declared in module common.widget, which does not export it)
1 error

As we have seen, the classes under packages that are not exported cannot be accessed even if they are public.

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/109214162