Description
Consider the following scenario:
// loaded via spring factories
public class SomeAutoConfiguration {
@ConditionalOnMissingBean
@Bean
public Converter<String, Integer> converter() {
// ...
}
}
// user configuration
@Configuration
public class SomeOtherConfiguration {
@Bean
public Converter<Integer, Float> someConverter() {
// ...
}
}
The @CondtionalOnMissingBean
annotated factory method will not be invoked as the conditional disregrads generic type information.
@ConditionalOnMissingBean
s JavaDoc states that @Bean
annotated methods default to the return type of the method for missing bean checks. A bean factories methods return types reflection data contains generic information which Spring Boot could use to do a more thorough check of existing beans. Currently Spring Boots OnBeanCondition
relies on SimpleMethodMetadata
which does not expose the generic return type of the factory method, so spring-core probably needs adjustments as well.
I think the @ConditionalOnMissingBean
condition should include generic type information in the future as Spring (Boot) supports autowring of generic types and has extended it's generic type support over the years which seems to be lacking in this area.
The only workaround is to use bean names instead of types, which feels off when building Spring Boot starters as users have to name their beans accordingly instead of just supplying a bean of the correct type.
Note: non-factory-method provided beans could also be supported via getGenericSuperclass()
based checks for @Component
s.