Skip to content

A modular monolith application in Spring Boot is when a traditionally monolithic application (with all code in one project/module) is broken up into distinct modules while still maintaining a single codebase and deployment.

Notifications You must be signed in to change notification settings

AadityaUoHyd/spring-boot-modulith-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Modulith Demo

This repository contains the code for the Spring Modulith.

What is Spring Boot Modulith

Spring Boot Modulith is a framework designed to support the development of modular monolithic applications in Spring Boot. It helps in structuring applications into distinct, modular parts while keeping them within a single deployable unit, hence maintaining the simplicity of monolithic deployments. This approach allows for better organization, maintainability, and potential future extraction of modules into microservices if needed.

Key Features of Spring Boot Modulith

  1. Modular Structure:

    • Encourages breaking down the application into modules with clear boundaries.
    • Modules can be developed and tested independently within the same application.
  2. Explicit Dependencies:

    • Facilitates explicit definition of dependencies between modules.
    • Helps in understanding and managing the interactions between different parts of the application.
  3. Isolation and Encapsulation:

    • Promotes encapsulation of logic within modules.
    • Reduces coupling between modules, making the application easier to maintain and evolve.
  4. Testing Support:

    • Provides tools and guidelines for writing tests at the module level.
    • Supports both unit and integration testing of modules.
  5. Transition to Microservices:

    • Modular monolithic design can serve as a stepping stone to microservices.
    • Modules can be extracted as independent services when needed.

Benefits

  • Improved Maintainability: Modular structure makes it easier to understand, maintain, and evolve the codebase.
  • Scalability: While starting as a monolith, the modular design allows for easier scaling by splitting modules into microservices when required.
  • Clear Separation of Concerns: Helps in organizing the codebase by separating different functionalities into distinct modules.

Example Usage

Here’s a brief overview of how you might structure a Spring Boot application using the Modulith approach:

  1. Define Modules: Organize your codebase into packages or modules, each representing a distinct part of the application (e.g., user, order, inventory).

  2. Set Up Dependencies: Use Spring's dependency injection to manage dependencies between modules.

  3. Encapsulate Logic: Ensure that each module encapsulates its logic and exposes only necessary functionality to other modules.

  4. Testing: Write unit and integration tests for each module to ensure that they function correctly in isolation and when integrated.

Example Project Structure

src/main/java/com/example
├── Application.java
├── catalog
│   ├── web -> controllers -> ProductController.java
│   ├── domain -> ProductEntity.java / ProductServiceImpl.java
│   ├── config -> CatalogExceptionHandler.java
│   └── ...
├── order
│   ├── web -> controllers -> OrderController.java
│   ├── domain -> OrderEntity.java / OrderServiceImpl.java
│   ├── config -> OrderExceptionHandler.java
│   └── ...
└── inventory
    ├── web -> controllers -> InventoryController.java
    ├── domain -> InventoryEntity.java / InventoryServiceImpl.java
    ├── config -> InventoryExceptionHandler.java
    └── ...

Example Configuration

  1. Application.java: Main class to bootstrap the Spring Boot application.
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. Catalog Module: Example of a catalog service within the catalog module.
@Service
public class ProductService {
    public String getProductDetails(String userId) {
        // Business logic for fetching user details
    }
}
  1. Order Module: Example of an order service within the order module.
@Service
public class OrderService {
    @Autowired
    private UserService userService;

    public String createOrder(String userId, String productId) {
        String userDetails = userService.getUserDetails(userId);
        // Business logic for creating an order
    }
}

Testing

  • Unit Tests: Test individual components within each module.
  • Integration Tests: Test the interaction between modules to ensure they work together as expected.

Summary

Spring Boot Modulith is a powerful framework that allows developers to build modular monolithic applications, providing a structured approach to organize code and manage dependencies. It offers a balance between the simplicity of monolithic applications and the organization and maintainability benefits of modular design, while also enabling a smooth transition to microservices if needed.

Source to study from :

About

A modular monolith application in Spring Boot is when a traditionally monolithic application (with all code in one project/module) is broken up into distinct modules while still maintaining a single codebase and deployment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages