In Spring Boot's documentation it is clearly stated that auto-configurations must be specified via the spring.factories
file:
Auto-configurations must be loaded that way only. Make sure that they are defined in a specific package space and that they are never the target of component scanning.
I did try to put a @Component
on my auto-configuration class and made sure it will be picked up by component scanning. It seems to work.
While I do think it's bad practice, since it is very unlikely that component scanning will actually pick it up in a real world scenario, I am wondering why the documentation feel so strongly about it. Are there any other dangers that I fail to anticipate, and if so, which?
Edit:
In https://youtu.be/jDchAEHIht0?t=734 Stéphane and Brian explain that there are two phases, one called "UserConfiguration Phase" and another "AutoConfiguration Phase". Following that thought would suggest, that using @ComponentScan
on an auto-configuration class would move it to the "User Configuration Phase" which would basically break semantics of auto-configuration.
However, I've not been able to break it in my experiments. As long as I keep my @Conditional
annotation it seems to work as expected...
spring.factories
are read. Those are read quite early in the proces and thus could influence the component-scanning or available beans/postprocessors etc. – Terricolous