In the early days of software architectures, many designers and practitioners often stressed the idea of modular code. Clearly, it makes sense to break up software services or APIs into modules; however, depending on who you talk to, the word module has different meanings to different people. For example, developers using Apache Maven build tools will often view jar artifacts as modules, while some would argue that packages within artifacts are also modules. So what does the word “module” mean in the Java world?
In this chapter, yoursquo;ll get to explore what modularity means for the new Java 9 platform. Yoursquo;ll also learn the Java platform module system (JPMS) in the context of JavaFX applications. This chapter is a gentle introduction to Java 9rsquo;s module system with common use case scenarios such as using a module as a library dependency.
Having said this, I will not be going into great detail covering every modularity use case (believe me, there are quite a few of them). Even as of this writing, there are ongoing discussions regarding other use cases, possibly out of the intended scope. For example, there are still ongoing discussions on the concept of automatic modules and how modules should be named. Despite the back and forth from expert groups and industry leaders relating to missing features versus scoped requirements, I trust the Java community will come to a (well-intended) consensus on a robust modular system that will satisfy most use cases out in the wild. To find out more on the topic of Java 9 Jigsaw, I have listed JSRs, books, and links in the appendix of this book.
As a general disclaimer, especially relating to the new Java 9rsquo;s platform module system, I will caution you with the following.
■ Disclaimer By the time you read this book, Java 9rsquo;s module system will be released. Since Java 9rsquo;s module system is new, the language specifications, APIs, terminologies, and concepts are subject to change. This chapter was written using Java 9rsquo;s early access edition.
Here is a list of sections to follow when reading this chapter on Java 9rsquo;s module system. If you already know the background about Java 9rsquo;s module system and its history, you can skip right to the section called “Getting Started.”
- What is Project Jigsaw?
- History (pre-Java 9)
- Getting Started
- Conclusion
copy; Carl Dea, Gerrit Grunwald, Joseacute; Pereda, Sean Phillips and Mark Heckler 2017 47
C. Dea et al., JavaFX 9 by Example, DOI 10.1007/978-1-4842-1961-4_2
ChAPTer 2 JAvAFX And JIgSAw
What Is Project Jigsaw?
Java modularity, aka “Project Jigsaw,” was an effort to enable developers to partition and encapsulate code in a more manageable manner on the Java platform. By way of partitioning code into modules, application code will be more loosely coupled instead one monolithic application. With good encapsulation, modules can be defined to expose (export) APIs to other modules safely. Javarsquo;s new module system will also be backward compatible to code and libraries prior to Java 9 (non-modular code). Not only are partitioning and encapsulating code very good principles, there are other additional benefits, as you will see later.
According to Mark Reinhold, he summarizes JSR 376 (Project Jigsaw):
The overall goal of this JSR is to define an approachable yet scalable module system for the Java Platform. It will be approachable, i.e., easy to learn and easy to use, so that developers can use it to construct and maintain libraries and large applications for both the Java SE and Java EE Platforms. It will be scalable so that it can be used to modularize the Java SE Platform itself, and its implementations.
Mark Reinhold Chief Architect of the Java Platform Group at Oracle, April 1, 2015
Benefits
According to the official web site of project Jigsaw (http://openjdk.java.net/projects/jigsaw) the following are the primary goals and benefits of a modular system on the Java platform:
- Make the Java SE platform, and the JDK, more easily scalable down to small computing devices
- Improve the security and maintainability of Java SE platform implementations in general, and the JDK in particular
- Enable improved application performance
- Make it easier for developers to construct and maintain libraries and large applications, for both the Java SE and EE platforms
To achieve these goals, we propose to design and implement a standard module system for the Java SE platform and to apply that system to the platform itself, and to the JDK. The module system should be powerful enough to modularize the JDK and other large legacy code bases, yet still be approachable by all developers.
Project Jigsaw site December 21, 2016
As mentioned relating to partitioning applications these benefits can also lead to scaling down applications to fit into smaller computing devices such as ARM based computers. Having modular code will also help the maintenance and security on the Java SE and Java EE platforms as microservices becomes the norm when developing on cloud and container-based application environments.
Since modules make way for reliable configuration building, applications would occupy smaller footprints, thus increasing the performance in areas relating to memory and load times. An example of this is a modular JavaFX applicationrsquo;s start-up speed boost due to a smaller set of modules loaded. Prior to Java 9, applications were developed and packaged with the entire Java runtime. These apps had to load many unused libraries such as corba or sql packages.
ChAPTer 2
剩余内容已隐藏,支付完成后下载完整资料
在软件架构的早期,许多设计师和实践者经常强调模块化代码的概念。显然,将软件服务或api分解成模块是有意义的;然而,根据你交谈的对象,“模块”这个词对不同的人有不同的含义。例如,使用Apache Maven构建工具的开发人员通常会将jar工件视为模块,而有些人则认为工件内的包裹也是模块。那么,“模块”这个词在Java世界中意味着什么呢?
在这一章中,您将了解模块化对新Java 9平台的意义。您还将在JavaFX应用程序的上下文中学习Java平台模块系统(JPMS)。本章是对Java 9的模块系统的一个温和的介绍,它具有常见的用例场景,例如使用模块作为库依赖。
说到这里,我将不会详细讨论每个模块化用例(相信我,有很多这样的例子)。即使在撰写本文时,仍有关于其他用例的讨论,可能超出了预期的范围。例如,关于自动模块的概念以及如何命名模块的讨论仍在进行中。尽管来自专家小组和行业领导者的一些与缺失的特性和作用域需求相关的问题,我相信Java社区将会在一个健壮的模块化系统上达成一个(良好的)共识,这个系统将满足大多数在野外的用例。为了了解更多关于Java 9 Jigsaw的主题,我在这本书的附录中列出了jsr、书籍和链接。
作为一个一般的免责声明,特别是与新的Java 9平台模块系统相关的,我将提醒您下面的内容。作为一个一般的免责声明,特别是与新的Java 9平台模块系统相关的,我将提醒您下面的内容。
■ 声明 在您阅读这本书的时候,免责声明将会被释放,Java 9的模块系统将会被释放。由于Java 9的模块系统是新的,所以语言规范、api、术语和概念都将发生变化。
这一章是使用Java 9的早期访问版本编写的。
下面是阅读本章关于Java 9模块系统的章节的列表。
如果您已经了解了关于Java 9模块系统及其历史的背景知识,您可以直接跳到“开始”这一节。”
bull;项目拼图是什么?
bull;历史(pre-Java 9)
bull;开始
bull;结论
copy; Carl Dea, Gerrit Grunwald, Joseacute; Pereda, Sean Phillips and Mark Heckler 2017 47
C. Dea et al., JavaFX 9 by Example, DOI 10.1007/978-1-4842-1961-4_2
ChAPTer 2 JAvAFX And JIgSAw
项目拼图是什么?
Java模块化,也就是“Project Jigsaw”,是为了让开发人员能够以一种更易于管理的方式在Java平台上对代码进行划分和封装。通过将代码划分为模块,应用程序代码将更加松散耦合,而不是一个单一的应用程序。通过良好的封装,可以将模块定义为安全地向其他模块公开(导出)api。Java的新模块系统在Java 9(非模块化代码)之前也会向后兼容代码和库。分区和封装代码不仅是非常好的原则,还有其他的好处,您稍后会看到。
根据Mark Reinhold的说法,他总结了JSR 376(Project Jigsaw):
这个JSR的总体目标是为Java平台定义一个可接近但可伸缩的模块系统。它将是可接近的,也就是说易于学习和易于使用,因此开发人员可以使用它来构造和维护Java SE和Java EE平台的库和大型应用程序。它将是可伸缩的,这样它就可以用来模块化Java SE平台本身及其实现。
Mark Reinhold Chief Architect of the Java Platform Group at Oracle, April 1, 2015
福利
根据project Jigsaw的官方网站(http://openjdkjava.net/projects/jigsaw),以下是Java平台上模块化系统的主要目标和好处:
bull;使Java SE平台和JDK更容易地扩展到小型计算设备
bull;提高Java SE平台实现的安全性和可维护性,特别是JDK
bull;提高应用程序性能
bull;让开发人员更容易地为Java SE和EE平台构建和维护图书馆和大型应用程序。
为了实现这些目标,我们建议为Java SE平台设计和实现一个标准模块系统,并将该系统应用于平台本身和JDK。模块系统应该足够强大,可以模块化JDK和其他大型遗留代码库,但所有开发人员仍然可以使用它。
Project Jigsaw site December 21, 2016
正如前面提到的分区应用程序,这些好处还可以减少应用程序,以适应更小的计算设备,比如基于ARM的计算机。模块化代码还将有助于Java SE和Java EE平台的维护和安全性,因为在基于云和容器的应用程序环境中,微服务成为规范。
由于模块为可靠的配置构建让路,应用程序将占用更小的内存,从而提高了内存和加载时间方面的性能。一个例子是模块化的JavaFX应用程序的启动速度提升,因为加载了一组更小的模块。在Java 9之前,应用程序被开发并打包成整个Java运行时。这些应用程序必须加载许多未使用的库,比如corba或sql包。
ChAPTer 2 JAvAFX And JIgSAw
模块化Java代码(Java 9)的额外好处被列为Java增强方案:
bull;模块化运行时图像(JEP 220在http://openjdk.java.net/jeps/220)
bull;用于模块的Java链接器(JEP 282 http://openjdk.java.net/jeps/282)
bull;提前编制(JEP 295:http://openjdkjava.net/jeps/295)
JEP 220是针对Java SE平台的模块化运行时映像的增强建议。架构师对整个Java运行时平台进行了重组,比如它的目录和核心库。重组运行时平台将有助于提高安全性、性能和可管理性。例如,诸如rt.jar、工具之类的库。jar和jfxrt。jar将不再是可访问的,但是都是在模块中被打破的。
Java 9中的新技术是jlink,Java linker(JEP 282)。jlink是一种通过将模块及其依赖关系链接到自定义运行时映像来组装和构建应用程序的工具。为了创建自定义运行时映像,Java linker与运行时的结构(JEP 220)结合使用。换句话说,这两个JEPs本质上是相互锁定的。
上面提到的3个JEP是JEP 295,在时间编译之前。它更广为人知的是AOT编译。尽管它被认为是实验性的,但是AOT编译器能够将Java代码转换成Linux操作系统上的本机代码。当环境没有安装Java运行时,这有一个很好的优势。另一个优势是速度。由于代码是静态编译的,所以不需要有JIT(只是在时间上)编译或warmups。该功能将用于小型设备或高效的服务器(网络设备)。当您阅读本文时,AOT编译将被支持
受欢迎的操作系统。
有了这三个重要的Java增强建议一起工作,您将开始看到基于Java的应用程序变得更简单、更薄、更快、更原生。
缺点
尽管设计模块化代码有很多好处,但也有一些缺点。以下是一些缺点:
bull;在定义模块和依赖项时附加的复杂性。
bull;没有针对多个模块的标准项目结构,因为它涉及各种构建工具和ide。
bull;将遗留的非模块化代码与模块化代码混合在一起时,类路径和模块路径正在发挥作用。调试代码和解决依赖关系可能会更加困难。
bull;遗留依赖性可能永远不会被改写成Java 9模块,这可能会导致向后或向前兼容性。
Java 9迁移路径
现在您已经了解了新的Java平台模块系统的优点和缺点,您可能仍然处于观望状态,因为未知的风险。也许你认为它还是太新了。为了减轻任何潜在的焦虑,Oracle已经创建了一个优秀的指南,帮助您将遗留Java应用程序迁移到Java 9。就去做吧!
迁移指南位于这里:https://docs.oracle.com/javas/9/迁移。另外,您还应该看看JDK 9中的重要变化:https://docs.oracle.com/javas/9/news/toc.htm。
该指南是对Java 9中发生了什么变化以及如何使用平台模块和第三方库的一个非常详细的解释。迁移指南还谈到了一种名为jdeps的分析工具。
jdeps分析工具
当对应用程序代码运行jdeps时,该工具将检查代码,以确定它是否依赖于弃用的依赖性或JDK内部api。例如,分析工具将建议应该替换的api。一些建议提供了使用从com.sun开始的包的代码替换。*,sun.misc。例如,如果您在引用诸如com.sun这样的非公共包时,创建了一个未模块化的Java应用程序。jdeps工具可以提供另一个模块来使用。下面是在调用jdeps工具来对抗Java 8编译类文件之后的输出。
$ jdeps -cp . com/jfxbe/helloworld/HelloWorld.class
HelloWorld.class -gt; java.base
HelloWorld.class -gt; javafx.base
HelloWorld.class -gt; javafx.controls
HelloWorld.class -gt; javafx.graphics
HelloWorld.class -gt; jdk.httpserver
com.jfxbe.helloworld -gt; com.sun.net.httpserver jdk.httpserver com.jfxbe.helloworld -gt; java.lang java.base com.jfxbe.helloworld -gt; javafx.application javafx.graphics com.jfxbe.helloworld -gt; javafx.collections javafx.base com.jfxbe.helloworld -gt; javafx.event javafx.base com.jfxbe.helloworld -gt; javafx.scene javafx.graphics com.jfxbe.helloworld -gt; javafx.scene.control javafx.controls com.jfxbe.helloworld -gt; javafx.stage javafx.graphics
分析的结果显示HelloWorld所使用的平台模块的摘要和详细信息。类文件。在那里您会注意到com.sun.net.httpserver可以在jdk中找到。httpserver模块。假设您的应用程序依赖于类com.sun.net.httpserver.httpserver,建议迁移到Java模块,这意味着您需要创建模块信息。需要jdk的java文件。httpserver模块作为一种依赖关系。
让我们看另一个使用jdeps工具来对抗Java 9模块化应用程序JAR文件的例子。
下面是一个通过显示依赖信息来分析模块化JAR应用程序的示例。
$ jdeps -v helloworld.jar
- -summary or -s Prints the dependency summary.
- -verbose or -v Prints all classes-level dependencies.
Herersquo;s the output after running jdeps:
$ jdeps -v mlib/com.jfxbe.helloworld.jar
com.jfxbe.helloworld
[file:///Users/cpdea/JavaFX9byExample/Code/jfx9be/chap02/helloworld/mlib/com.jfxbe.
helloworld.jar] requires mandated java.base (@9-ea) requires javafx.base (@9-ea) requires javafx.controls (@9-ea) requires javafx.graphics (@9-ea) requires jdk.httpserver (@9-ea)
ChAPTer 2 JAvAFX And JIgSAw
com.jfxbe.helloworld -gt; java.base com.jfxbe.helloworld -gt; javafx.base com.jfxbe.helloworld -gt; javafx.controls com.jfxbe.helloworld -gt; javafx.graphics com.jfxbe.helloworld -gt; jdk.httpserver com.jfxbe.helloworld.HelloWorld -gt; com.jfxbe.helloworld.HelloWorld$1 com.jfxbe. helloworld
com
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[463464],资料为PDF文档或Word文档,PDF文档可免费转换为Word
以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。