Skip to content

Consider adding a hierarchical/overridable bean resolution mechanism for auto-configurations #27864

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

Closed
grubeninspekteur opened this issue Sep 3, 2021 · 3 comments
Labels
status: duplicate A duplicate of another issue

Comments

@grubeninspekteur
Copy link

grubeninspekteur commented Sep 3, 2021

Motivation: Given a library's auto-configuration requiring a bean of a specific type to be present in the application context, the resolution will fail if there is more than one bean present. One could annotate one of the beans as @Primary, however there are cases when you want the auto-configuration to pick up the secondary (not @Primary annotated) bean.

Suggestion: Allow providing a "scoped" or "hierarchical" bean qualification mechanism for a specific auto-configuration and/or configuration classes in a specific package, such that bean resolution works as if there was an explicit @Qualifier annotation present on all injection points.

Example SPI:

@CustomizeAutoConfiguration(for = SomeLibraryAutoconfiguration.class)
public class SomeLibraryCustomzier implements AutoConfigurationCustomizer {

    @Autowired
    @Qualifier("secondDbEntityManager")
    private EntityManager entityManager;

    @Override
    public void customizeBeanResolution(BeanResolutionCustomizer resolution) {
      resolution.forAutowiringOf(EntityManager.class).useBean(this.entityManager);
    }

}

This could also help solving issue #22403 , e.g. using something like resolution.hideExistingBean(ObjectMapper.class) on Spring's auto-configuration class that provides an ObjectMapper.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 3, 2021
@bertondeng
Copy link

I think this will make the bean-injection mechanism a little bit sophisticated.

@grubeninspekteur
Copy link
Author

@bertondeng Do you mean as in "too complex" / "harder to follow"?

@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label Oct 8, 2021
@philwebb
Copy link
Member

Thanks for the suggestion, but we need to consider more use-cases for #22403 before we can implement any fix. We also want to involve the Spring Framework team since we expect that changes may be required to the core.

I'll close this one for now as a duplicate of #22403

@philwebb philwebb added status: duplicate A duplicate of another issue 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 Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants