Java 9 मॉड्यूल सिस्टम (Project Jigsaw) मजबूत एनकैप्सुलेशन लागू करता है: डिफ़ॉल्ट रूप से, एक मॉड्यूल में कोई भी सार्वजनिक प्रकार मॉड्यूल के बाहर पहुंच योग्य नहीं है। module-info.java में export क्लॉज विशिष्ट पैकेजों को अन्य मॉड्यूल्स के लिए दृश्यमान बनाता है।
निर्यात क्लॉज के प्रकार
1. योग्य निर्यात: exports <package> to <module>, ...
सबसे प्रतिबंधात्मक - केवल सूचीबद्ध मॉड्यूल्स पैकेज तक पहुंच सकते हैं।
module com.tutorialspoint.model {
// केवल com.tutorialspoint.gui और com.tutorialspoint.service पहुंच सकते हैं
exports com.tutorialspoint.model.entity to
com.tutorialspoint.gui,
com.tutorialspoint.service;
// आंतरिक पैकेज - किसी को भी निर्यात नहीं
// exports com.tutorialspoint.model.internal; // नहीं!
}
2. सार्वजनिक निर्यात: exports <package>
कम प्रतिबंधात्मक - सभी मॉड्यूल्स (नामित और अनाम) पैकेज तक पहुंच सकते हैं।
module com.tutorialspoint.model {
// कोई भी मॉड्यूल इस पैकेज का उपयोग कर सकता है
exports com.tutorialspoint.model.api;
// आंतरिक कार्यान्वयन छुपा हुआ रहता है
// exports com.tutorialspoint.model.impl; // नहीं!
}
पूर्ण module-info.java उदाहरण
module com.tutorialspoint.application {
// निर्भरताएं
requires com.tutorialspoint.model;
requires java.sql;
requires java.logging;
// सार्वजनिक API निर्यात करें
exports com.tutorialspoint.app.api;
exports com.tutorialspoint.app.dto;
// केवल विशिष्ट मित्र मॉड्यूल्स के लिए आंतरिक API निर्यात करें
exports com.tutorialspoint.app.internal.config to
com.tutorialspoint.admin,
com.tutorialspoint.test;
// सेवाएं
uses com.tutorialspoint.spi.PaymentProcessor;
provides com.tutorialspoint.spi.PaymentProcessor
with com.tutorialspoint.impl.StripeProcessor;
// रिफ्लेक्शन के लिए खोलें (सीमित)
opens com.tutorialspoint.app.model to
java.persistence,
com.fasterxml.jackson.databind;
}
महत्वपूर्ण नियम
एक पैकेज = एक मॉड्यूल
package exists in another module
एक पैकेज केवल एक मॉड्यूल में मौजूद हो सकता है। यदि दो मॉड्यूल एक ही पैकेज निर्यात करते हैं, तो संकलन/रनटाइम विफल होता है।
module A { exports com.example.pkg; }
module B { exports com.example.pkg; } // त्रुटि!
सब-पैकेज स्वचालित रूप से निर्यात नहीं होते
module example {
exports com.example.api; // केवल com.example.api
// com.example.api.internal स्वचालित रूप से निर्यात नहीं होता!
// com.example.api.v2 स्वचालित रूप से निर्यात नहीं होता!
// स्पष्ट रूप से निर्यात करना होगा:
exports com.example.api.internal;
exports com.example.api.v2;
}
exports बनाम opens
| क्लॉज | उद्देश्य | कंपाइल-टाइम | रनटाइम (रिफ्लेक्शन) |
|---|---|---|---|
exports pkg | सार्वजनिक प्रकारों तक पहुंच | ✅ अनुमत | ✅ सार्वजनिक सदस्य |
opens pkg | गहरी रिफ्लेक्शन पहुंच | ❌ नहीं | ✅ सभी सदस्य (निजी सहित) |
opens pkg to M | योग्य गहरी रिफ्लेक्शन | ❌ नहीं | ✅ मॉड्यूल M के लिए सभी सदस्य |
व्यावहारिक पैटर्न
API/कार्यान्वयन पृथक्करण
module com.example.library {
// सार्वजनिक API - सभी के लिए दृश्यमान
exports com.example.library.api;
exports com.example.library.spi;
// आंतरिक कार्यान्वयन - छुपा हुआ
// exports com.example.library.internal; // जानबूझकर छोड़ा गया
// टेस्ट के लिए आंतरिक खोलें
opens com.example.library.internal to
com.example.library.test,
org.junit.jupiter.api;
}
मित्र मॉड्यूल्स (योग्य निर्यात)
module com.example.core {
// केवल भाई मॉड्यूल्स के लिए आंतरिक API
exports com.example.core.internal to
com.example.web,
com.example.batch,
com.example.integration;
}
सेवा प्रदाता पैटर्न
module com.example.service {
exports com.example.service.api;
uses com.example.service.api.Processor;
provides com.example.service.api.Processor
with com.example.service.impl.DefaultProcessor;
}
कमांड-लाइन सत्यापन
# मॉड्यूल विवरण दिखाएं (निर्यात शामिल)
java --describe-module com.tutorialspoint.model
# आउटपुट:
# exports com.tutorialspoint.model.api
# exports com.tutorialspoint.model.entity to com.tutorialspoint.gui, com.tutorialspoint.service
# requires java.logging mandatory
# uses com.tutorialspoint.spi.Processor
संकलन और रनटाइम जांच
# संकलन - मॉड्यूल पथ का उपयोग करें
javac -d out --module-source-path src $(find src -name "*.java")
# रनटाइम - मॉड्यूल पथ
java --module-path out -m com.tutorialspoint.app/com.tutorialspoint.app.Main
# जांचें कि कौन से मॉड्यूल एक पैकेज पढ़ सकते हैं
jdeps --module-path out --require com.tutorialspoint.model
सामान्य गलतियां
- बहुत अधिक निर्यात करना: केवल वास्तविक API निर्यात करें, आंतरिक कक्षाएं नहीं
- sub-packages भूलना: प्रत्येक sub-package को स्पष्ट रूप से निर्यात करना होगा
opensका अत्यधिक उपयोग: रिफ्लेक्शन के लिए केवल आवश्यकता होने पर उपयोग करें- चक्रीय निर्यात: मॉड्यूल A, B से निर्यात करता है, B, A से निर्यात करता है - अनुमत लेकिन डिजाइन गंध
सारांश
exports pkg- सार्वजनिक API, सभी मॉड्यूल्स के लिए दृश्यमानexports pkg to M1, M2- योग्य निर्यात, केवल सूचीबद्ध मॉड्यूल्स- एक पैकेज ठीक एक मॉड्यूल से संबंधित है
- सब-पैकेज स्वचालित रूप से निर्यात नहीं होते
- गहरी रिफ्लेक्शन पहुंच के लिए
opensका उपयोग करें - मजबूत एनकैप्सुलेशन = बेहतर रखरखाव, स्पष्ट अनुबंध