-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@ConditionalOn(Missing)Bean(annotation = …) infers the type to match when used on a @Bean method #42484
Comments
For your conditional to work, it has to be evaluated after Lines 56 to 59 in 689bce8
You should move the definition of |
There was a little bit more to this due to the condition being used on a @ConditionalOnBean(annotation = MyAttribute.class)
@Bean
public Bar bar() {
return new Bar("stuff");
} This usage means that the following applies: Lines 38 to 51 in 99142db
As a result, the condition was looking for a bean of type To look for a bean of any type that is annotated with package com.example.demo.lib;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@AutoConfiguration
@ConditionalOnBean(annotation = MyAttribute.class)
public class MyAutoConfiguration {
@Bean
public Bar bar() {
return new Bar("stuff");
}
} |
Re-opening as there's more to this still. When used on a We could change the behavior by skipping the inference when I'd like to discuss this with the rest of the team and also consider what, if anything, we should do for |
We discussed this today and we think this is a bug, but one that's too risky for a patch release. We're going to remove the inference entirely for |
In hindsight, I'm not sure we should do this part. It's pretty unlikely, but possible that a user wants to create two beans of the same type when one is present. |
Bug report
We’ve been running into an issue with the
@ConditionalOnBean(annotation = XXX)
annotation with Spring Boot. It seems that the conditional doesn’t work if the bean with the conditional is the only bean of that type being registered. Attached is a sample project to demonstrate.spring-boot-conditional-annotation.tar.gz
The text was updated successfully, but these errors were encountered: