Skip to content
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

Consider generics in @ConditionalOnMissingBean when possible #28845

Closed
roookeee opened this issue Nov 29, 2021 · 1 comment
Closed

Consider generics in @ConditionalOnMissingBean when possible #28845

roookeee opened this issue Nov 29, 2021 · 1 comment
Labels
status: superseded An issue that has been superseded by another

Comments

@roookeee
Copy link

roookeee commented Nov 29, 2021

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 29, 2021
@philwebb philwebb added for: team-meeting An issue we'd like to discuss as a team to make progress type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged for: team-meeting An issue we'd like to discuss as a team to make progress labels Nov 29, 2021
@philwebb philwebb added this to the General Backlog milestone Dec 1, 2021
@philwebb
Copy link
Member

philwebb commented Feb 3, 2022

Closing in favor of PR #29500

@philwebb philwebb closed this as completed Feb 3, 2022
@philwebb philwebb added status: superseded An issue that has been superseded by another and removed type: enhancement A general enhancement labels Feb 3, 2022
@philwebb philwebb removed this from the General Backlog milestone Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

3 participants