Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A team wants to enforce module boundaries inside a Spring Boot monolith. Compare ArchUnit, Java modules (JPMS) and Spring Modulith for this goal.

ArchUnit expresses any dependency rule as a test and fits existing code with almost no change, and FreezingArchRule lets it fail only on new violations. JPMS enforces boundaries in the compiler and the JVM, but needs module descriptors, usually separate build modules, and is awkward with reflection-heavy frameworks like Spring. Spring Modulith treats each top-level package as a module, verifies dependencies between them, and adds events, testing and documentation support. The choice depends on how strict the enforcement must be and how much restructuring we can afford.

Why this question is asked

A Spring Boot monolith is not a problem in itself; a monolith with no internal boundaries is. When every class can call every other class, the code turns into one tangle that nobody can change safely, and splitting it into services later becomes almost impossible. Teams that want to avoid this look for tools that make boundaries enforceable. Interviewers ask this comparison to see whether we understand what each tool really enforces, what it costs, and how to choose. In this answer, we will recap what a module boundary is, look at each of the three options with a small example, compare them side by side, and finish with how to decide.

1A Quick Recap of Module Boundaries

A module, in this sense, is a part of the application with a clear responsibility, for example orders, inventory and billing. A boundary means two things: other modules may only use the module's public API, not its internals, and the dependencies between modules form a sensible direction, ideally without cycles. If orders depends on billing and billing depends on orders, they are effectively one module.

In Java, the usual tool is the package, but Java's public keyword is too coarse: a public class in com.acme.billing.internal is visible to everyone. So we need something extra to say which classes are the API, and to check that the rules hold.

2ArchUnit: Rules as Tests

ArchUnit imports our compiled classes and lets us write rules about them in Java. Those rules run as unit tests. It can express almost anything: no cycles between top-level packages, internal packages only used from their own module, controllers never calling repositories.

Java
@AnalyzeClasses(packages = "com.acme.shop")
class BoundaryTest {
@ArchTest
static final ArchRule noCycles =
slices().matching("com.acme.shop.(*)..").should().beFreeOfCycles();
@ArchTest
static final ArchRule internalsStayInside = FreezingArchRule.freeze(
noClasses().that().resideOutsideOfPackage("..billing..")
.should().dependOnClassesThat().resideInAPackage("..billing.internal.."));
}

Notice that nothing about the application changes: no new build modules, no annotations in the main code. That is ArchUnit's strength for an existing monolith. FreezingArchRule records today's violations in a store, fails only on new ones and shrinks the store as violations are fixed, so we can start enforcing a boundary tomorrow even if 60 classes break it today. The limitation is that it is a test: it catches violations at build time, but nothing stops a forbidden call at compile time in the IDE, and the rules are only as good as what we write.

3JPMS: Boundaries in the Compiler and the JVM

The Java Platform Module System, added in Java 9, lets a JAR declare itself a module in module-info.java: which modules it requires and which packages it exports. Packages not exported are inaccessible to other modules, enforced by javac and by the JVM at runtime, even through reflection unless the package is opened.

Java
// billing/src/main/java/module-info.java
module com.acme.shop.billing {
requires com.acme.shop.orders; // allowed dependency
exports com.acme.shop.billing.api; // only this package is visible
opens com.acme.shop.billing.internal to spring.core, spring.beans; // reflection for Spring
}
// in another module:
// import com.acme.shop.billing.internal.InvoiceCalculator;
// error: package com.acme.shop.billing.internal is not visible

This is the strongest enforcement of the three: illegal code does not compile. But the cost for a monolith is high. Each module must be its own JAR, so a single Maven module becomes many, and split packages, the same package in two JARs, are not allowed. Spring relies on reflection to create beans, inject fields and build proxies, so internal packages must be opened to Spring's modules, which weakens the encapsulation we wanted. In practice, most Spring Boot applications run on the class path, where module descriptors are ignored, so JPMS gives compile-time checks only if the whole build is set up for it.

4Spring Modulith: Packages as Application Modules

Spring Modulith is a Spring project built for exactly this problem. It treats each direct sub-package of the main application package as an application module. Classes in the module's root package are its API; classes in nested packages are internal. A single test verifies the structure.

Java
// com.acme.shop.ShopApplication is the main class, so:
// com.acme.shop.orders, com.acme.shop.billing, com.acme.shop.inventory are application modules
class ModularityTest {
@Test
void verifiesModuleStructure() {
ApplicationModules modules = ApplicationModules.of(ShopApplication.class);
modules.verify(); // fails on cycles and on access to internals
new Documenter(modules).writeDocumentation(); // module diagrams and a module canvas
}
}
// in billing: react to an orders event instead of calling orders directly
@ApplicationModuleListener
void on(OrderCompleted event) { invoices.createFor(event.orderId()); }
Three ways to enforce module boundaries
AspectArchUnitJPMSSpring Modulith
Enforcedin tests at build timeby javac and the JVMin tests at build time
Change to codenone, rules live in testsmodule-info and separate JARspackage convention, maybe moves
Rule styleany rule we can writeexports and requires onlyconvention plus allowed dependencies
Existing violationsFreezingArchRulemust fix before compilingmust fix, or rules relaxed
Fits Spring reflectionyesawkward, needs opensyes, built for Spring
Extrasgeneral architecture rulesstrong runtime encapsulationevents, module tests, docs
Stronger enforcement costs more restructuring.

Under the hood, Spring Modulith uses ArchUnit for verification, so we get the same test-time enforcement with a convention instead of hand-written rules. On top of that it adds things that help a modular monolith grow: @ApplicationModuleTest to start only one module and its dependencies in a test, an event publication registry so events published by one module and handled by another survive a crash, and generated documentation of the modules and their relationships. @ApplicationModule(allowedDependencies = ...) restricts which modules may be used. The cost is that our packages must follow its convention, which may mean moving classes.

5How to Choose

The decision comes down to two questions: how hard must the boundary be, and how much restructuring can we afford? If the code base is large and tangled and we need to stop things getting worse right now, ArchUnit with frozen rules is the fastest start, because it needs no changes to the main code. If the team is designing module boundaries and wants a convention, tooling and events that make a modular monolith pleasant to work in, Spring Modulith is the best fit for a Spring Boot application, and it can be adopted gradually by first aligning packages. JPMS I would reserve for libraries or platform components where runtime encapsulation really matters; for a Spring Boot monolith, the build restructuring and reflection workarounds rarely pay off.

These are not exclusive. A common path is ArchUnit first to stop the bleeding, then reorganising packages towards Spring Modulith modules, with extra ArchUnit rules for things Modulith does not cover, such as layering inside a module.

6Follow-up questions

Can Spring Modulith help us split the monolith into services later?

It makes that much easier, because modules already talk through a public API and events rather than reaching into each other's internals. Extracting a module whose only inbound traffic is events and a small API is far simpler than untangling direct calls everywhere. It does not do the extraction for us, though.

Why not simply use multiple Maven modules?

Separate build modules do enforce dependencies at compile time, because a module cannot use what is not on its class path. But they do not hide internals inside a module, they make the build heavier, and moving code between them is slow. Many teams combine a few build modules for big boundaries with ArchUnit or Modulith inside them.

Does FreezingArchRule hide problems?

It records them rather than hiding them: the store is a file in the repository listing every known violation, and it only shrinks as violations are fixed. The risk is forgetting about it, so it helps to track its size over time.

7Conclusion

In this answer, we compared three ways to enforce module boundaries in a Spring Boot monolith. ArchUnit writes arbitrary rules as tests and fits existing code, with FreezingArchRule to tolerate today's violations. JPMS enforces boundaries in the compiler and JVM but needs module descriptors and separate JARs and fights Spring's reflection. Spring Modulith treats top-level packages as modules, verifies them with ArchUnit underneath, and adds events, module tests and documentation. We choose by the strength of enforcement we need and the restructuring we can afford.

What to say in the interviewAbout 80 seconds aloud

Open with

I would compare them on how strongly they enforce and what they cost. ArchUnit is a library that reads the compiled classes and runs rules as unit tests.

Then, if they want more

It can express any rule, like no cycles between top-level packages or no access to internal packages, and it needs no change to the main code. With FreezingArchRule it records existing violations and fails only on new ones, so it is the fastest way to stop an existing monolith getting worse.

JPMS is the strongest: with module-info, non-exported packages are not visible to the compiler or the JVM. But it requires each module to be its own JAR, no split packages, and Spring's reflection means opening internal packages, which undoes part of the benefit, so I would not use it for a Spring Boot monolith.

Spring Modulith is designed for this case. Each direct sub-package of the main application package is a module, the root package is its API and sub-packages are internal, and ApplicationModules verify checks it, using ArchUnit underneath. It also adds module-scoped tests, a reliable event publication registry and generated documentation.

My usual path is ArchUnit with frozen rules first, then moving towards Spring Modulith as the packages are cleaned up.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A team wants to enforce module boundaries inside a Spring Boot monolith. Compare...": https://scrivio-interview-library.netlify.app/java/code-quality-tools/269a7f781c/