Allow @Bean method to override scanned class matching its return type

Closes gh-31052
This commit is contained in:
Juergen Hoeller
2023-08-15 13:55:57 +02:00
parent d89e305c87
commit 57f675c537
2 changed files with 46 additions and 11 deletions

View File

@@ -301,8 +301,12 @@ class ConfigurationClassBeanDefinitionReader {
}
// A bean definition resulting from a component scan can be silently overridden
// by an @Bean method, as of 4.2...
if (existingBeanDef instanceof ScannedGenericBeanDefinition) {
// by an @Bean method - and as of 6.1, even when general overriding is disabled
// as long as the bean class is the same.
if (existingBeanDef instanceof ScannedGenericBeanDefinition scannedBeanDef) {
if (beanMethod.getMetadata().getReturnTypeName().equals(scannedBeanDef.getBeanClassName())) {
this.registry.removeBeanDefinition(beanName);
}
return false;
}