Skip to content

Improve documentation of how @ConditionalOnBean evaluates all of its attributes #15177

Closed
@bcalmac

Description

@bcalmac

Consider the following (simplified) auto-configuration from a starter project:

@Configuration
public class MyStarterConfiguration {

    @Bean
    @ConditionalOnBean(annotation = EnableSomething.class)
    @ConditionalOnMissingBean(Integer.class)
    public Integer myBean() {
        return 10;
    }

    @Bean
    public String myOtherBean(Integer myBean) {
        return myBean.toString();
    }
}

Assuming that the application is annotated with @EnableSomething and no Integer bean is defined, you'd expect that myBean above would be instantiated. Indeed this works fine in 1.5.x. However, in 2.x the auto-configuration fails as below:

The following candidates were found but could not be injected:
	- Bean method 'myBean' in 'MyStarterConfiguration' not loaded because @ConditionalOnBean (types: java.lang.Integer; SearchStrategy: all) did not find any beans of type java.lang.Integer

Instead of just looking for a bean annotated with @EnableSomething, it looks for a bean of type Integer. Is there a different pattern for using @EnableXXX style annotations in 2.x?

Here is a sample project: https://github.com/bcalmac/conditional-on-annotation. mvn test should fail and it would work if you change the parent to 1.5.x.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions