JDK模块化之多模块示例

JDK模块化四之多模块示例

JDK模块化系列文章
JDK的模块化之Overview
JDK模块化之模块的基础概念
JDK模块化之简单示例
JDK模块化之多模块示例

多模块示例

源码下载 CSDN下载地址

演示一个依赖于另一个定制模块和标准模块的模块,新建项目Multi-Module。它的主要组成:

  • 主模块com.donny.client,它使用com.donny.widget模块组件来呈现所需传递的消息,并返回特定类型的定制消息以打印它
  • 消息类型在org.donny.data模块中。

项目结构为三个模块和五个包,组织方式如下:

  • 主模块包含一个主程序,com.donny.client,包含一个包:com.donny.main
  • 支持模块,com.donny.widget,包含两个软件包。com.donny.renderercom.donny.support
  • 数据模块,org.donny.data包含两个包:org.donny.dataorg.donny.data.type

以下是模块开发以及模块和api如何相互交互的计划步骤:

  • 开发一个模块,导出一个包供其他模块使用
  • 在另一个开发模块中使用导出包中的类
  • 导出指定的包,供指定模块使用
  • 编译、JAR并运行主模块

图解步骤

项目新建

新建Empty Project项目,命名为Multi-Module,确认JDK为17。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
新建数据模块支持模块主模块
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

开发数据模块

在src下新建包org.donny.data并新建类Message.java,新建包org.donny.data.type,在包下新建类Type.java

package org.donny.data;

import org.donny.data.type.Type;

import java.util.Objects;

public class Message {
    private String message;
    private Type type;

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        if (Objects.isNull(message) || message.isEmpty()) {
            throw new IllegalArgumentException("Invalid content;message should not be Null,or empty!");
        }
        this.message = message;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }
}
package org.donny.data.type;

public enum Type {
    XML,
    JSON,
    STRING
}

在src下面新建module-info.java。导出模块包org.donny.dataorg.donny.data.type只给模块com.donny.widget可用。

module org.donny.data {
    exports org.donny.data;
    exports org.donny.data.type to com.donny.widget;
}
开发支持模块

在src下面新建module-info.java。导出模块包org.donny.support,依赖数据模块org.donny.data并传递该依赖,

module com.donny.widget {
    exports com.donny.support;
    requires transitive org.donny.data;
}

在src下新建包com.donny.support并新建类RendererSupport.java,新建包com.donny.renderer,在包下新建类SimpleRenderer.java

package com.donny.renderer;


import org.donny.data.Message;

public class SimpleRenderer {
    public void renderAsString(Message message) {
        System.out.println(processMessage(message));
    }
    private String processMessage(Message msg){
        return String.format("%n {Message= %s', Type= %s}",
                msg.getMessage(),
                msg.getType().toString());
    }
}

package com.donny.support;


import com.donny.renderer.SimpleRenderer;
import org.donny.data.Message;

import static org.donny.data.type.Type.STRING;

public class RendererSupport {
    private final Message message = new Message();

    public void render(String message) {
        this.message.setMessage(message);
        this.message.setType(STRING);
        new SimpleRenderer().renderAsString(this.message);
    }

    public Message getCurrentMessage() {
        return this.message;
    }
}

开发主模块

在src下新建文件module-info.java。依赖com.donny.widget

module com.donny.client {
    requires com.donny.widget;
}

在src下新建包com.donny.client并且新建类Client.java

package com.donny.client;

import com.donny.support.RendererSupport;

public class Client {
    public static void main(String[] args) {
        RendererSupport support = new RendererSupport();
        support.render("Hello! Welcome to the Java 17 Platform Module System");
        System.out.printf("%n %s %n %s %n %s %n %s", "-----------------",
                support.getCurrentMessage().getMessage(), "-----------------",
                support.getCurrentMessage().getType());
    }
}

运行Client的main方法。

 {Message= Hello! Welcome to the Java 17 Platform Module System', Type= STRING}

 ----------------- 
 Hello! Welcome to the Java 17 Platform Module System 
 ----------------- 
 STRING

完整的工程目录
在这里插入图片描述

打包成jar

配置Artifacts

打开Project Structure,选择Artifacts,新增选择JAR from modules with dependencies)。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Build

回到主窗体,选择Build, 单机Build artifacts,选择all。
在这里插入图片描述

运行client.jar

通过以下命令执行

java -p mlib -m com.donny.client/com.donny.client.Client

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值