Quick revision
The short answer
Give each module one public entry point, a configuration class or an @Enable annotation, and keep its other classes package-private. The application then imports the module configurations explicitly instead of scanning everything, modules talk only through published interfaces or events, and each module is tested with only its own configuration loaded. Spring Modulith can verify these rules automatically so they do not erode.
What to remember
In this answer, we designed configuration for a modular monolith. Each module exposes one public configuration class or @Enable annotation and a small public API, and keeps its internals package-private. The application imports module configurations explicitly instead of scanning everything, modules talk only through public interfaces or events, each module can be tested with only its own configuration, and Spring Modulith verifies the rules in the build so they last.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Give each module one public entry point, a configuration class or an @Enable annotation, and keep its other classes package-private. The application then imports the module configurations explicitly instead of scanning everything, modules talk only through published interfaces or events, and each module is tested with only its own configuration loaded. Spring Modulith can verify these rules automatically so they do not erode.
Why this question is asked
A modular monolith is one deployable application split into modules with clear boundaries, such as orders, billing and catalog. The boundaries are what make it better than a big ball of mud, and in a Spring application they are easy to break by accident: one broad component scan and any class can inject any other. Interviewers ask this design question to see whether we can turn module boundaries into something the framework and the build enforce. In this answer, we will look at why scanning everything erodes boundaries, then design a module's configuration, compose the application, decide how modules talk, test one module alone, and finally let Spring Modulith check the rules.
1Why Scanning Everything Erodes Boundaries
By default, @SpringBootApplication scans its own package and every package below it. In a monolith with com.shop.orders, com.shop.billing and com.shop.catalog, that means every @Component in every module becomes a bean that any other bean can inject. Nothing stops OrderService from injecting BillingRepository directly and reading billing tables.
Each shortcut like that looks harmless, but after a year the modules are tangled: changing billing's schema breaks orders, and tests for one module need the whole application. So the goal of the design is to make the right dependencies easy and the wrong ones impossible, or at least visible.
2Designing One Module's Configuration
Each module gets one public configuration class and a small public API. Everything else, the service implementation, the repository, the entities, is package-private, so Java itself stops other modules from referring to it.
Notice that the module may still use scanning inside itself; that is convenient and harmless, because the scan cannot leave the module's package. What other modules see is BillingApi and BillingModuleConfiguration. If we prefer, the configuration class can be wrapped in an @EnableBilling annotation that imports it, which reads nicely in the application class.
3Composing the Application Explicitly
The application class should no longer scan everything. We put it in its own package, such as com.shop.app, so its default scan finds nothing in the modules, and it imports each module configuration by name.
- ShopApplicationimports the module configurations by name
- Module entry pointspublic configuration class or
@Enableannotation per module - Module APIs and eventspublic interfaces and event records other modules may use
- Module internalspackage-private services, repositories and entities
- Shared infrastructureBoot auto-configuration:
DataSource, transactions, web
5 layers, from the top down. Step through them one at a time.
Only the top three layers are visible across modules; internals stay inside their package.
Now the list of modules in the application is written down in one place. Adding a module is a deliberate one-line change, and removing one shows immediately which other modules depended on it, because their constructors will fail to find a bean. One practical detail: JPA entity scanning and repository scanning also need to cover the module packages, so each module configuration declares its own @EntityScan and @EnableJpaRepositories for its package, or the application lists them explicitly. Check how this interacts with your Boot version's defaults before relying on them.
4How Modules Talk to Each Other
Modules communicate in two ways. For a request that needs an answer, the caller depends on the other module's public interface, such as BillingApi, and Spring injects the implementation. For something that has happened, such as an order being placed, the owning module publishes an event, for example a record OrderPlaced(OrderId id, Money total), and other modules listen to it.
The choice between them is about direction of knowledge. A direct call means orders knows billing exists. An event means orders only announces a fact and does not know who reacts. So we use calls where the caller needs the result now, and events for side effects other modules care about. Either way, the contract is a public type owned by one module, and modules never reach into each other's tables or beans.
5Testing One Module at a Time
Because each module has its own configuration, a test can load only that module and replace its neighbours with test doubles.
A module test that needs half the application to start is a sign that a boundary has leaked. Spring Modulith offers @ApplicationModuleTest, which bootstraps just one module, and optionally its direct dependencies, without writing the configuration list by hand.
6Letting Spring Modulith Verify the Boundaries
Conventions erode under deadline pressure, so we let a test enforce them. Spring Modulith treats each direct subpackage of the application's main package as a module, by default, and can check that no module uses another module's internal types and that there are no dependency cycles.
Notice that Modulith's default module detection is based on packages under the main application class, so if we moved ShopApplication into com.shop.app as above, we point Modulith at the right base package or keep the application class at the root and narrow its scan instead. Check the Modulith documentation for your version. The important part is that the rule now fails the build, which turns a design document into a guarantee.
7Follow-up questions
Is explicit @Import worth it if Spring Modulith already verifies boundaries?
They solve different problems. Modulith tells us when a boundary is crossed; explicit imports make the composition readable and let tests and alternative applications, such as a batch runner, load a chosen subset of modules. Many teams use Modulith with normal scanning and are happy; explicit imports are most useful when we really assemble modules differently.
Where do shared things like a Money type go?
Into a small shared module that every module may depend on and that depends on none of them. It should hold only stable value types and utilities, because anything placed there couples every module to it.
How does this help if we later split a module into a service?
Because the module already talks to others only through a public interface and events, splitting means replacing the interface implementation with a remote client and the events with broker messages. The code that used the interface does not change, which is the main payoff of the discipline.
8Conclusion
In this answer, we designed configuration for a modular monolith. Each module exposes one public configuration class or @Enable annotation and a small public API, and keeps its internals package-private. The application imports module configurations explicitly instead of scanning everything, modules talk only through public interfaces or events, each module can be tested with only its own configuration, and Spring Modulith verifies the rules in the build so they last.
Open with
The main risk in a Spring monolith is that one broad component scan makes every bean visible to every module, so boundaries erode. I would give each module one public entry point: a configuration class, or an @Enable annotation that imports it, plus a small public API of interfaces and event types.
Then, if they want more
Everything else, services, repositories, entities, stays package-private, so Java itself prevents other modules from using it. The module can still scan inside its own package. The application class would sit in its own package and import the module configurations explicitly, so the list of modules is visible in one place and adding one is a deliberate change.
Modules talk through the public interfaces when they need an answer, and through events, which are records owned by the publishing module, when they announce something happened. They never share tables or reach into each other's beans.
For tests, each module is loaded alone with its configuration and its neighbours mocked; if a module test needs the whole application, the boundary has leaked. Finally, I would add Spring Modulith's verify test, which fails the build when a module uses another module's internals or when modules form a cycle, and use its documenter to generate module diagrams. That turns the design into a rule the build enforces.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- Spring Framework reference, Core technologies
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 "How would you structure configuration in a large modular monolith so that each module...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/2b67946b49/