๐ Block by block, a forest grows. After a period of discussion and practice around modular structures, the Spring community introduced Spring Modulith as a set of conventions for exactly that! ๐
Spring Modulith is simple and effective in its own right. Its goal is to implement logical modules in Spring Boot projects, support well-defined structural dependencies, improve layering and isolation, and provide verification, documentation, testing, and observability support. โจ
๐ ๏ธ What Is Spring Modulith? ๐๏ธ
๐ญ In Spring Modulith, an Application Module is a logical unit that includes:
- Public API: beans or events that other modules can call or consume;
- Internal components: should not be used directly by other modules;
- Required interfaces: dependencies on functionality provided by other modules.
๐ฌ๏ธ The goal of modularization is to enforce layering, control dependencies, and keep each unit self-contained.
๐ก Quick Module Analysis ๐ข
๐ To create a module model, all you need is:
var modules = ApplicationModules.of(Application.class);
modules.forEach(System.out::println);
๐ The module information it outputs:
## example.inventory ##
> Base package: example.inventory
> Spring beans: InventoryManagement, SomeInternalComponent
๐ Module grouping is derived from package-structure analysisโclean and natural.
๐ Module Types: Simple and Advanced ๐
- Simple modules: direct subpackages; all public classes form the API, while internals are isolated via package-private access;
- Advanced modules: have subpackages and an internal group that isolates components that shouldn't be depended on.
๐น Nested Module Design ๐ท๏ธ
๐ If a module needs another layer of structure inside it, you can mark a subpackage as a nested module:
@ApplicationModule
package example.inventory.nested;
๐ ๏ธ A nested module can access its parent module, but outside code cannot access it.
๐ก๏ธ Open Modules: A Migration Tool for Legacy Projects ๐๏ธ
๐ When migrating a legacy project, you can mark a module as OPEN:
@ApplicationModule(type = Type.OPEN)
package example.inventory;
๐ This allows other modules to access internal classes, but you should handle it incrementally on the path to full isolation.
๐ข Declaring Module Dependencies Explicitly ๐
๐ To limit which modules can be depended on, use allowedDependencies:
@ApplicationModule(allowedDependencies = "order")
package example.inventory;
๐ If you only want to depend on a specific Named Interface, use "order :: spi".
๐ Named Interfaces and Exposed Interfaces ๐
๐ผ Mark a specific package as a Named Interface:
@NamedInterface("spi")
package example.order.spi;
๐ This lets other modules depend only on that interface.
๐ Simplified Configuration: @Modulithic ๐
๐ฃ Use it on the Spring Boot main class:
@Modulithic(systemName = "Inventory Management", sharedModules = {"common"}, additionalPackages = {"com.example.common"})
@SpringBootApplication
class MyApplication {}
๐ Manage module configuration in one placeโsimple and efficient.
๐จโ๐ผ Custom Module Detection Strategy ๐
๐ If you need custom module discovery:
class CustomApplicationModuleDetectionStrategy implements ApplicationModuleDetectionStrategy {
@Override
public Stream<JavaPackage> getModuleBasePackages(JavaPackage basePackage) {
// ๆ นๆฎๅฝๅ่งๅ้ๆฉ
}
}
๐ข Configure it with:
spring.modulith.detection-strategy=example.CustomApplicationModuleDetectionStrategy
๐ Conclusion ๐
๐ Spring Modulith is a simple and effective modularization tool, well suited to improving structural isolation and maintainability in Spring Boot applications! ๐ซ
๐ It doesn't require major changes to existing code, while laying the groundwork for future layering and evolution toward services. โจ