Detect illegal bean definition override during classpath scanning
Closes gh-25952
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -97,6 +97,19 @@ public interface BeanDefinitionRegistry extends AliasRegistry {
|
||||
*/
|
||||
int getBeanDefinitionCount();
|
||||
|
||||
/**
|
||||
* Determine whether the bean definition for the given name is overridable,
|
||||
* i.e. whether {@link #registerBeanDefinition} would successfully return
|
||||
* against an existing definition of the same name.
|
||||
* <p>The default implementation returns {@code true}.
|
||||
* @param beanName the name to check
|
||||
* @return whether the definition for the given bean name is overridable
|
||||
* @since 6.1
|
||||
*/
|
||||
default boolean isBeanDefinitionOverridable(String beanName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given bean name is already in use within this registry,
|
||||
* i.e. whether there is a local bean or alias registered under this name.
|
||||
|
||||
@@ -1011,7 +1011,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
|
||||
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
|
||||
if (existingDefinition != null) {
|
||||
if (!isAllowBeanDefinitionOverriding()) {
|
||||
if (!isBeanDefinitionOverridable(beanName)) {
|
||||
throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
|
||||
}
|
||||
else if (existingDefinition.getRole() < beanDefinition.getRole()) {
|
||||
@@ -1040,8 +1040,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
}
|
||||
else {
|
||||
if (isAlias(beanName)) {
|
||||
if (!isAllowBeanDefinitionOverriding()) {
|
||||
String aliasedName = canonicalName(beanName);
|
||||
String aliasedName = canonicalName(beanName);
|
||||
if (!isBeanDefinitionOverridable(aliasedName)) {
|
||||
if (containsBeanDefinition(aliasedName)) { // alias for existing bean definition
|
||||
throw new BeanDefinitionOverrideException(
|
||||
beanName, beanDefinition, getBeanDefinition(aliasedName));
|
||||
@@ -1150,8 +1150,19 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns {@code true} if bean definition overriding
|
||||
* is generally allowed.
|
||||
* @see #setAllowBeanDefinitionOverriding
|
||||
*/
|
||||
@Override
|
||||
public boolean isBeanDefinitionOverridable(String beanName) {
|
||||
return isAllowBeanDefinitionOverriding();
|
||||
}
|
||||
|
||||
/**
|
||||
* Only allows alias overriding if bean definition overriding is allowed.
|
||||
* @see #setAllowBeanDefinitionOverriding
|
||||
*/
|
||||
@Override
|
||||
protected boolean allowAliasOverriding() {
|
||||
@@ -1164,7 +1175,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
@Override
|
||||
protected void checkForAliasCircle(String name, String alias) {
|
||||
super.checkForAliasCircle(name, alias);
|
||||
if (!isAllowBeanDefinitionOverriding() && containsBeanDefinition(alias)) {
|
||||
if (!isBeanDefinitionOverridable(alias) && containsBeanDefinition(alias)) {
|
||||
throw new IllegalStateException("Cannot register alias '" + alias +
|
||||
"' for name '" + name + "': Alias would override bean definition '" + alias + "'");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user