tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

[ISSUE] ConditionalOnBean should only be used on auto-configuration classes

Open bclozel opened this issue 1 year ago • 1 comments
trafficstars

Article and Module Links

https://www.baeldung.com/spring-conditional-annotations#predefined-conditional-annotations https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-annotations-2

Describe the Issue

The article promotes the use of @ConditionalOnBean on actual components or bean declaration in user configuration.

This is explicitly called out as an invalid case in the Spring Boot Javadoc:

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.

Please remove this case from samples and articles.

bclozel avatar Feb 17 '24 10:02 bclozel

Hey @bclozel,

Thanks for the heads up. We'll update the article to fix this.

Regards, Team.

apeterlic avatar Feb 28 '24 06:02 apeterlic