News/Tech News

Modular Spring Boot 3 Applications with Modules and Events

Published on Nov 21, 2022

Spring Modulith, an experimental project from VMware, aims to better structure monolithic Spring Boot 3 applications through modules and events. This project introduces new classes and annotations, but does not generate any code. Rather than using the Java Platform Module System (JPMS), its modules map to plain Java packages. The modules have an API, but Spring Modulith recommends using application events as the “primary method of interaction.” These events can be automatically persisted to an event log. Additionally, Spring Modulith facilitates the testing of modules and events.

Spring Modulith is based on the upcoming Spring Boot 3 framework, which is scheduled for release in November 2022. Therefore, it is based on Spring Framework 6, Java 17, and Jakarta EE 9. Spring Modulith is the successor project to Moduliths (with a trailing “s”). This project used Spring Boot 2.7 and is now retired, receiving only bug fixes until November 2023.

Because Java packages are not hierarchical, Spring Modulith introduces its module abstraction. Therefore, in the sample code below, the SomethingOrderInternal class from the example.order.internal package is visible to all classes, not just those in the example.order package.

As a result, Spring Modulith cannot cause Java compilation to fail due to a violation of its module access rules. Instead, it utilises unit tests: ApplicationModules.of(Application.class).verify() fails in the example above if another module attempts to access the module-internal class SomethingOrderInternal. In order to provide this capability, Spring Modulith relies on the ArchUnit project.

Spring Modulith encourages the use of Spring Framework application events for intermodule communication. This event management system enhances these events by providing an Event Publication Registry that ensures the delivery of events by persisting them. Regardless of whether the entire application crashes or only a module receiving the event crashes, the registry still delivers the event. The registry supports a variety of serialization formats and defaults to JSON. JPA, JDBC, and MongoDB are the out-of-the-box persistence methods.

Spring Modulith, an experimental project from VMware, aims to better structure monolithic Spring Boot 3 applications through modules and events. This project introduces new classes and annotations, but does not generate any code. Rather than using the Java Platform Module System (JPMS), its modules map to plain Java packages. The modules have an API, but Spring Modulith recommends using application events as the “primary method of interaction.” These events can be automatically persisted to an event log. Additionally, Spring Modulith facilitates the testing of modules and events.

The Spring Boot 3 framework, which is due in November 2022, serves as the basis for Spring Modulith. As a result, it has a baseline of Spring Framework 6, Java 17, and Jakarta EE 9. The Spring Modulith project is the successor to Moduliths (with a trailing “s”). The project used Spring Boot 2.7 and is now retired, receiving only bug fixes until November 2023.

Because Java packages are not hierarchical, Spring Modulith introduces its module abstraction. Therefore, in the sample code below, the SomethingOrderInternal class from the example.order.internal package is visible to all classes, not just those in the example.order package.

As a result, Spring Modulith cannot cause Java compilation to fail due to a violation of its module access rules. Instead, it utilises unit tests: ApplicationModules.of(Application.class).verify() fails in the example above if another module attempts to access the module-internal class SomethingOrderInternal. In order to provide this capability, Spring Modulith relies on the ArchUnit project.

Spring Modulith encourages the use of Spring Framework application events for intermodule communication. This event management system enhances these events by providing an Event Publication Registry that ensures the delivery of events by persisting them. Regardless of whether the entire application crashes or only a module receiving the event crashes, the registry still delivers the event. The registry supports a variety of serialisation formats and defaults to JSON. JPA, JDBC, and MongoDB are the out-of-the-box persistence methods.

The testing of events is also improved. This example illustrates how the new PublishedEvents abstraction can be used to filter received events to OrderCompleted with a particular ID.

The Spring Modulith can automatically publish events such as HourHasPassed, DayHasPassed, and WeekHasPassed at the end of a particular period (such as an hour, day, or week). Passage of Time events provide a convenient alternative to duplicating Spring @Scheduled annotations with cron triggers.

As the Spring ecosystem offers plenty of options for coordinating events, Spring Modulith does not include a workflow, choreography, or orchestration component.

Spring Modulith automatically creates Micrometer spans for module API durations and event processing using the new observability support in Spring Framework 6. AsciiDoc files created by Spring Modulith can also be used to document modules, including C4 and UML component diagrams for the relationship between modules, and an Application Module Canvas for the content of a single module, such as Spring beans.

Tech News

Angular 15

Angular 15 arrives with standalone APIs

With the new standalone APIs, now released from developer preview, Angular developers…

analytics in TigerGraph Cloud

Machine learning, graph analytics in TigerGraph Cloud

With the updated graph database-as-a-service (DBaaS), visual analytics and machine learning…

Our Latest Blog

Unlock Your Potential with a Level 5 Diploma in Business London's Top Courses img

Unlock Your Potential with a Level 5 Diploma in Business: London’s Top Courses

Are you looking to enhance your knowledge and skills in the field of business? Do...
Read More
Unlock Your Potential with Level 4 Diploma in Business Courses in London img

Unlock Your Potential with Level 4 Diploma in Business Courses in London

Are you looking for a comprehensive course to take your business career to the next...
Read More

Follow Us

Resources

Presentations
Browse LSET presentations to understand interesting…

Explore Now


eBooks
Get complete guides to empower yourself academically…

Explore Now


Infographics
Learn about information technology and business…

Explore Now