简体   繁体   中英

Should Spring Boot auto-configuration classes use the @Configuration annotation?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM