Considering the following configuration class which declares two beans, bean B
which should only be created on conditional of bean A
.
@Configuration(
proxyBeanMethods = false
)
@AutoConfigureBefore(SomeOtherCOnfig.class)
@ConditionalOnProperty(name = "prop",havingValue = "false")
public class SomeConfiguration {
@Bean("beanA")
BeanA beanA() {
if (someCOndition){
return null;
}
return new BeanA();
}
@ConditionalOnBean(name = "beanA")
@Bean(name = "beanB")
BeanB beanB(@Qualifier("beanA") BeanA beanA) {
return new BeanB(beanA);
}
When someCondition
is true I expect beanA
not to be matched in the autoconfiguration report thus mean beanB creation is never attempted to be executed.
The behaviour i see is, beanA
is considered a positive match in the autoconfiguration
SomeConfiguration#beanB matched:- @ConditionalOnBean (names: beanA; SearchStrategy: all) found bean 'beanA' (OnBeanCondition)
and beanB
is attempted to be created and as I expect happens the exception thrown
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
Why is the beanA
considered to be in the beanFactory here during autoconfiguration but also not there either when injecting dependencies?
Is it because the bean is there, or at least a reference of type which points to null null? But if that is the case shouldn't spring be injecting a null bean to beanB