Based on my testing, I've found no obvious difference when Spring Boot auto-configuration classes have or don't have the @Configuration
annotation - if they are configured in spring.factories
correctly they are loaded regardless of the @Configuration
annotation.
However, it seems like every custom auto-configuration example and demo uses the @Configuration
annotation. Is there a reason all these examples use @Configuration
(or is it just convention)? Is there any impact to not using @Configuration
?
Some examples that use @Configuration
on auto-configuration classes:
Yes, they should be. The documentation states that an auto-configuration class should be annotated with @Configuration
:
Under the hood, auto-configuration is implemented with standard
@Configuration
classes. Additional@Conditional
annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use@ConditionalOnClass
and@ConditionalOnMissingBean
annotations.
If you do not annotate them with @Configuration
you are relying upon Spring Framework’s “lite” @Bean mode. This changes the behaviour of the auto-configuration classes. It is not guaranteed to work in the future as the implementation does not meet the documented requirements.