Skip to content

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

@bclozel

Description

@bclozel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    on-jiratriagedIssues reviewed by a dev and considered valid. Will be added in Jira.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions