Skip to content

Consider generics in @ConditionalOnMissingBean when possible #28845

Closed
@roookeee

Description

@roookeee

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.

@ConditionalOnMissingBeans 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 @Components.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions