Description
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
.