Java का Modules System
In this page:
module module.name {
requires other.module;
exports package.name;
}
Introduction to Modules
Java Module System, जो मूल रूप से Project Jigsaw के अंतर्गत विकसित हुआ, आपको संबंधित packages को explicit, declared dependencies वाले एक single, reusable module में group करने देता है। यह किसी module को यह सीमित करने भी देता है कि उसके कौन से internal packages बाहरी दुनिया को दिखाई दें, पूरे classpath को scan करने से बचकर security और startup performance दोनों सुधारते हुए।
उदाहरण: Introduction to Modules
public class Main {
public static void main(String[] args) {
// A module groups packages with explicit, declared dependencies (see module-info.java)
System.out.println("Module system groups related packages together");
}
}
Login to try C/C++/Java/PHP code in the editor
Module Descriptors
एक module अपने root पर रखी एक module-info.java file से define होता है, जो specify करता है कि module के कौन से packages वह दूसरे modules को उपयोग के लिए exports करता है, और बदले में compile और run करने के लिए किन दूसरे modules की उसे requires है।
उदाहरण: Module Descriptors
public class Main {
public static void main(String[] args) {
// module-info.java would declare:
// module com.example.app {
// exports com.example.app.api;
// requires java.sql;
// }
System.out.println("module-info.java declares exports and requires");
}
}
Login to try C/C++/Java/PHP code in the editor
Loading Platform Modules
आप सभी currently loaded platform modules को list करने के लिए चल रही JVM से query कर सकते हैं, जो यह समझने का एक उपयोगी तरीका है कि कोई दिया गया application runtime पर JDK के किन हिस्सों तक असल में access रखता है।
उदाहरण: Loading Platform Modules
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a stream from the collection to process it step by step
ModuleLayer.boot().modules().stream()
.limit(3)
.forEach(m -> System.out.println(m.getName()));
}
}
Login to try C/C++/Java/PHP code in the editor
Service Loader API
Module System मौजूदा ServiceLoader API के साथ सीधे integrate होता है, modules को explicitly यह declare करने देते हुए कि वे कौन सी services provide करते हैं और कौन सी consume करते हैं। यह modules को loosely coupled रखता है, क्योंकि consuming module को कभी अपनी उपयोग की जा रही service की concrete class जानने की ज़रूरत नहीं पड़ती।
उदाहरण: Service Loader API
import java.util.ServiceLoader;
public class Main {
interface Greeter {
String greet();
}
static class EnglishGreeter implements Greeter {
public String greet() { return "Hello"; }
}
public static void main(String[] args) {
Greeter g = new EnglishGreeter(); // ServiceLoader.load(Greeter.class) would discover providers declared in module-info.java
System.out.println(g.greet());
}
}
Login to try C/C++/Java/PHP code in the editor
Module Layer API
आप किसी चल रही JVM के भीतर active module layers को inspect कर सकते हैं, जहां हर layer पहले से resolved modules के एक graph को उन्हें load करने के लिए ज़िम्मेदार class loaders के साथ represent करता है, जो ज़्यादातर plugin-style या modular application architectures बनाते समय relevant होता है।
उदाहरण: Module Layer API
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
ModuleLayer bootLayer = ModuleLayer.boot();
// Print a line to the console
System.out.println("Boot layer module count: " + bootLayer.modules().size());
}
}
Login to try C/C++/Java/PHP code in the editor
module-info.javaमें किसी package कोexportsकरना भूल जाना, जिससे दूसरे modules उसकी classes को नहीं देख पाते भले ही वे public हों।- किसी matching
requiresline के बिना दूसरे module की classes का उपयोग करना, जो 'package is not visible' compile error देता है। - यह उम्मीद करना कि reflection किसी module के private members तक पहुंच सकेगा, जबकि reflection का उपयोग करने वाले frameworks के लिए आपको package को
opensकरना चाहिए।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: