Spring Modulith is relatively new framework. There are some pitfalls and frequent misconceptions that you might encounter when working with it - especially if you have a background in the “regular Spring Boot” way of doing things. This page is intended as a loose collection of issues to be aware of when working with Spring Modulith, and how to avoid them.
The most common pitfall when working with Spring Modulith is using the wrong annotations. Spring Modulith is built on top of Spring Boot, but has in part a different set of annotations - sometimes confusingly named the same way as their Spring Boot counterparts. If you are not extra-careful, the easy way of adding imports in an IDE like IntelliJ might lead you to import the wrong annotation.
@SpringBootApplication
for your main classWhen using Spring Modulith, you should not use the @SpringBootApplication
annotation on your
main class. Instead, you should use the @Modulith
annotation. The @Modulith
annotation is
a meta-annotation that includes the @SpringBootApplication
annotation, but also adds some
additional configuration that is necessary for Spring Modulith to work correctly.
@Service
annotationWhen creating a service class in Spring Modulith, it is marked by the @Service
annotation.
This, however is not the @Service
annotation from Spring Boot (org.springframework.stereotype.Service
).
Instead, you need to import org.jmolecules.ddd.annotation.Service
.
@SpringBootTest
When writing tests for Spring Modulith, you should not use the @SpringBootTest
annotation.
Instead, you should use the @ApplicationModuleTest
annotation (org.springframework.modulith.test.ApplicationModuleTest
).
@AggregateRoot
annotation instead of deriving your class from AggregateRoot
This is not a pitfall, but maybe worth mentioning. When creating an aggregate root
in Spring Modulith, you have two ways of doing it. You can either use the @AggregateRoot
annotation,
or you can derive your class from org.jmolecules.ddd.types.AggregateRoot
.
Although both ways should be valid, all code examples I could find use the derivation approach, not
the annotation. (Drotbohm talks about this in the interview with Eberhard Wolff.)
I haven’t tried the annotation with @AggregateRoot
myself.
This page is a work in progress. I will add more pitfalls as students (or me) will encounter them.