To modularize a monolithic Java application using Java 9's Module System, divide the application into independent business-focused modules and define their relationships using module-info.java files. Each module should expose only the APIs that other modules need while keeping internal implementation details hidden. This creates a cleaner architecture, simplifies dependency management, and improves long-term maintainability.
Key Points: • Break the monolithic application into logical modules such as user, payment, order, and reporting based on business functionality. • Define module dependencies explicitly using requires and expose public APIs using exports in module-info.java. • Strong encapsulation prevents unauthorized access to internal classes, improving security and reducing coupling.
Example: An e-commerce application can be split into separate modules such as Customer, Product, Order, and Payment. Each module manages its own functionality and communicates with other modules through well-defined interfaces rather than direct access to internal classes.
Code Example:
module com.ecommerce.order {
requires com.ecommerce.customer;
requires com.ecommerce.payment;
exports com.ecommerce.order.api;
}
module com.ecommerce.payment {
exports com.ecommerce.payment.api;
}Interview Tip: A concise interview answer is: To modularize a monolithic application, I would identify business domains, create separate Java modules for each domain, define dependencies using module-info.java, and expose only required APIs. This improves maintainability, reduces coupling, enhances security through encapsulation, and makes the application easier to scale and manage.