CH12 - Modules Flashcards
(19 cards)
What is module
A module is a group of one or more packages plus a special file called module-info.java
Module declaration Rules
- The module-info.java file must be in the root directory of your module. Regular Java classes should be in packages.
- The module declaration must use the keyword module instead of class, interface or enum.
- The module name follows the naming rules for package names. It often includes periods (.) in its name. Regular class and package names are not allowed to have dashes (-). Module names follow the same rule
export
It is used to indicate that a module intends for those packages to be used by Java code outside the module.
Java module
It is a packaging mechanis that enables you to package a Java application or Java API as a separate Java module.
A Java module can specify which of the Java packages it contains that should be visible to other Java modules which uses this module.
Modules contain
A java module is one or more Java packages that belong together. A module could be either a full Java application, a Java Platform API, or third party API>
Module exports
A Java module must explicitly export all packages int he module that are to be accessible for other modules using the module.
module com.jenkov.mymodule {
exports com.jenkov.mymodule;
}
No “subpackages” of the exported package are exported.
Parent package export
You dont have to export the parent package in order to export a subpackage.
module com.jenkov.mymodule {
exports com.jenkov.mymodule.util;
}
This example only exports the com.jenkov.mymodule.util package, and not the com.jenkov.mymodule package.
Module requires
If a Java module requires another module to do its work, that module must be specified in the module descriptior too.
module com.jenkov.mymodule {
requires javafx.graphics;
}
Circular Dependencies Not Allowed
It is not allowed to have circular dependencies between modules. In other words, If module A requires module B, then module B cannot also require module A. The module dependency graph must be an acyclic graph.
Split Packages Not Allowed
The same Java package can only be exported by a single Java module at runtime.
Having two modules export the same package is also sometimes referred to as a split package. By split package is meant that the total content (classes) of the package is split between multiple modules. This is not allowed
🔧 What is a Service in Java Modules (JPMS)?
In JPMS, a Service is a way to allow one module to provide implementations of an interface, and another module to consume (use) them — without tightly coupling the two modules.
It’s part of the Service Provider Interface (SPI) mechanism, now integrated into the module system.
JPMS key Concepts
🧩 Key Concepts
provides – a module provides an implementation of a service.
uses – a module uses a service.
ServiceLoader – used at runtime to load the implementations.
Service Implementation Module
A Java module that wants to implement a service interface from a service interface module must:
-Require the service interface module in its own module descriptor.
-Implement the service interface with a Java class.
-Declare the service interface implementation in its module descriptor.
Automatic Module
It appears on the module path but does not contain a module-info.java file. It is simply a regular JAR file that is placed on the module path and gets treated as a module
A service
A service consists of the service provider interface and logic to look up implementations of using a service locator.
Automatic modules
Automatic modules are on the module path but do not have a module-info.java file
Named modules
Named modules are on the module path and do have a module-info.java.
Unnamed modules
Unnamed modules are on the classpath.
A cyclic dependency
A cyclic dependency is when a module graph forms a circle.