Check for alias overriding bean definition of same name

Closes gh-25430
This commit is contained in:
Juergen Hoeller
2020-07-22 18:42:53 +02:00
parent 32b35e9a23
commit 718d46adac
2 changed files with 46 additions and 3 deletions

View File

@@ -95,8 +95,7 @@ import org.springframework.util.StringUtils;
* operating on pre-resolved bean definition metadata objects.
*
* <p>Note that readers for specific bean definition formats are typically
* implemented separately rather than as bean factory subclasses:
* see for example {@link PropertiesBeanDefinitionReader} and
* implemented separately rather than as bean factory subclasses: see for example
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
*
* <p>For an alternative implementation of the
@@ -1106,6 +1105,18 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return isAllowBeanDefinitionOverriding();
}
/**
* Also checks for an alias overriding a bean definition of the same name.
*/
@Override
protected void checkForAliasCircle(String name, String alias) {
super.checkForAliasCircle(name, alias);
if (!isAllowBeanDefinitionOverriding() && containsBeanDefinition(alias)) {
throw new IllegalStateException("Cannot register alias '" + alias +
"' for name '" + name + "': Alias would override bean definition '" + alias + "'");
}
}
@Override
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
super.registerSingleton(beanName, singletonObject);