ARTICLE AD BOX
I have a Java Spring Boot application structured into multiple modules. Currently, the application decides at startup which modules to activate based on configuration properties. While this works, I’m concerned about resource efficiency:
Some modules are rarely used by clients.
Yet, the application carries all active modules and their beans in memory at all times.
I want to redesign the application to meet these goals:
Lazy module loading: A module should only be loaded when it’s first needed at runtime.
Complete module removal: When a module is inactive, all its beans and components should be completely removed from the application context to reduce memory footprint.
I know there will be probably some trade-offs, like cold starts when a rarely used module is first loaded.
I know about Spring Modulith, but as far as I understand, it doesn’t support unloading modules on demand. Please correct me if I’m wrong.
My questions are:
Is it possible to implement dynamic loading and unloading of modules with Spring Boot, including all beans and components?
Are there any existing Spring-based libraries or patterns that can help achieve this behavior?
If it’s feasible, what would be the recommended approach (e.g., dynamic application contexts, Classloader tricks, etc.)?
I’m heavily using Spring Beans and Components, so ideally the solution should be Spring-friendly.
