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);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -62,6 +62,8 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@ -375,6 +377,18 @@ public class ConfigurationClassPostProcessorTests {
.withMessageContaining(TestBean.class.getName());
}
@Test // gh-25430
public void detectAliasOverride() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
beanFactory.setAllowBeanDefinitionOverriding(false);
context.register(FirstConfiguration.class, SecondConfiguration.class);
assertThatIllegalStateException().isThrownBy(context::refresh)
.withMessageContaining("alias 'taskExecutor'")
.withMessageContaining("name 'applicationTaskExecutor'")
.withMessageContaining("bean definition 'taskExecutor'");
}
@Test
public void configurationClassesProcessedInCorrectOrder() {
beanFactory.registerBeanDefinition("config1", new RootBeanDefinition(OverridingSingletonBeanConfig.class));
@ -1266,6 +1280,24 @@ public class ConfigurationClassPostProcessorTests {
}
}
@Configuration
static class FirstConfiguration {
@Bean
SyncTaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
}
@Configuration
static class SecondConfiguration {
@Bean(name = {"applicationTaskExecutor", "taskExecutor"})
SimpleAsyncTaskExecutor simpleAsyncTaskExecutor() {
return new SimpleAsyncTaskExecutor();
}
}
public static class ScopedProxyConsumer {
@Autowired