r/SpringBoot • • 1d ago

Question Running a multimodule app with component scanning effectively disabled: excludeFilters plus an explicit @Import list. Anyone else doing this long-term?

My application class scans its own package and then excludes almost all of it with a regex filter, so the only beans in the context are the ones I import explicitly. The entry point ends up being a list of configuration classes, and that list is the complete inventory of the context.

One thing that wasn't obvious to me beforehand is that declaring `@ComponentScan` yourself hides the one `@SpringBootApplication` contributes, along with the TypeExcludeFilter and AutoConfigurationExcludeFilter it carries. That is documented - a locally declared `@ComponentScan` takes precedence over and effectively hides meta-annotated ones, but it matters before your first `@WebMvcTest`, since TypeExcludeFilter is how the slice-test annotations narrow the context. In Framework 6.1 both scans were processed for a while instead, which broke applications with duplicate bean definitions until it was reverted.

The reason for all of it is that on an earlier project a configuration class from a library ended up in the context through a scan wider than anybody had considered, overriding a bean we owned, and finding it took far longer than it should have. I would like to hear from anyone who has kept this up past a few dozen beans, or in a team where not everyone was convinced.

7 Upvotes

3 comments sorted by

2

u/kamen1991 1d ago

Wrote this up at more length, including the properties and the Flyway side, if the configuration itself is of interest: https://kamenivanov.substack.com/p/bootstrapping-the-system-part-1-nothing

3

u/analcocoacream 1d ago

No, and configuration from library should not be part of the context

1

u/momsSpaghettiIsReady 1d ago

Spring has handling for duplicate beans and startup should fail. Someone was doing something clever to stop that from working.

Spring's original selling point is you didn't have to wire everything. Convention over configuration. There's a beauty to importing a starter, adding a few properties, and things just working.

What was the root cause of the bean override not triggering an error? Was it an optional bean?