Spring Modulith 1.0: A New Way to Build Modular Spring Boot Applications

Spring Modulith 1.0, a project that helps developers to structure their Spring Boot applications into modules and events, has recently been released as a fully supported Spring project. The project aims to improve the testability, maintainability, and documentation of Spring Boot applications by enforcing a clear separation of concerns and responsibilities among the modules.

What is Spring Modulith?

Spring Modulith is a project that allows developers to build well-structured Spring Boot applications and guides them in finding and working with application modules driven by the domain. It supports the verification of such modular arrangements, integration testing of individual modules, observing the application’s behavior on the module level, and creating documentation snippets based on the arrangement created.

Spring Modulith 1.0: A New Way to Build Modular Spring Boot Applications
Spring Modulith 1.0: A New Way to Build Modular Spring Boot Applications

Spring Modulith is based on the concept of modular monoliths, which are monolithic applications that are internally divided into modules that communicate through well-defined interfaces and events. This way, the modules can be developed, tested, and deployed independently, while still benefiting from the simplicity and consistency of a single codebase.

Spring Modulith leverages the existing package structure of Spring Boot applications to define the modules and their dependencies. It also provides annotations and configuration properties to customize the module behavior and metadata. For example, developers can use @Module to mark a package as a module, @ModuleDependency to declare a dependency on another module, @PublishedEvents to specify the events published by a module, and @EnableModuleVerification to enable automatic verification of the module structure.

What’s New in Spring Modulith 1.0?

Spring Modulith 1.0 was promoted from its experimental status and became a fully supported Spring project in June 2023. This means that it ships with a couple of breaking changes:

  • The group identifier is now org.springframework.modulith (previously org.springframework.experimental).
  • The Spring Boot baseline of the project is 3.1 (previously 3.0).
  • The name of the actuator endpoint exposing the application module structure is now application-modules (previously applicationmodules).
  • The previously deprecated configuration properties in the JDBC-based event registry have been removed.

The release also includes a couple of bug fixes and new features over 0.6, which can be found in the official change log. Some of the notable features are:

  • Support for jMolecules annotations, which are a set of annotations that describe common architectural concepts such as bounded contexts, aggregates, entities, value objects, repositories, services, etc. Spring Modulith can detect these annotations and generate application documentation that groups classes by their annotated role, such as “Port” or “Adapter”.
  • Support for Spring Native, which is a project that allows developers to compile their Spring applications to native executables using GraalVM. Spring Modulith can be used with Spring Native to create modular native applications that have faster startup time and lower memory footprint.
  • Improved IDE support through actuators that expose the module structure and metadata in JSON format. IDEs such as Spring Tool Suite and Visual Studio Code can use these actuators to visualize the modules and their dependencies in a graphical way.

How to Use Spring Modulith?

To use Spring Modulith, developers need to follow these steps:

  • Create a Spring Boot application using [Spring Initializr] or any other tool.
  • Create a Java package arrangement that puts business modules as direct sub-packages of the application’s main package. Each module should contain its own classes and resources related to its domain.
  • Annotate the packages with @Module and optionally provide additional information such as name, displayName, description, etc.
  • Declare the dependencies between modules using @ModuleDependency on the package level or on individual classes.
  • Specify the events published by each module using @PublishedEvents on the package level or on individual classes.
  • Enable module verification by adding @EnableModuleVerification on the application class or any configuration class. This will check if the module structure is consistent and follows the best practices.
  • Run integration tests for individual modules using @ApplicationModuleTests, which will bootstrap only the required modules and their dependencies for testing.
  • Write documentation snippets using Documenter, which will generate Asciidoctor files with diagrams and tables based on the module structure.

Why Use Spring Modulith?

Spring Modulith offers several benefits for developers who want to build modular Spring Boot applications, such as:

  • It enforces a clear separation of concerns and responsibilities among the modules, which improves the readability, maintainability, and testability of the code.
  • It allows developers to focus on the domain logic rather than the technical details of wiring up components and dependencies.
  • It provides tools for verifying, testing, observing, and documenting the modules and their interactions, which increases the quality and reliability of the application.
  • It integrates well with other Spring projects and technologies, such as Spring Data, Spring Security, Spring Cloud, Spring Native, etc.
  • It is easy to use and requires minimal configuration and boilerplate code.

Spring Modulith 1.0 is a milestone release that marks the project’s maturity and stability. It is a valuable addition to the Spring ecosystem and a useful tool for developers who want to build modular Spring Boot applications.

Leave a Reply

Your email address will not be published. Required fields are marked *