Skip to content

Commit 718d46a

Browse files
committed
Check for alias overriding bean definition of same name
Closes gh-25430
1 parent 32b35e9 commit 718d46a

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@
9595
* operating on pre-resolved bean definition metadata objects.
9696
*
9797
* <p>Note that readers for specific bean definition formats are typically
98-
* implemented separately rather than as bean factory subclasses:
99-
* see for example {@link PropertiesBeanDefinitionReader} and
98+
* implemented separately rather than as bean factory subclasses: see for example
10099
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
101100
*
102101
* <p>For an alternative implementation of the
@@ -1106,6 +1105,18 @@ protected boolean allowAliasOverriding() {
11061105
return isAllowBeanDefinitionOverriding();
11071106
}
11081107

1108+
/**
1109+
* Also checks for an alias overriding a bean definition of the same name.
1110+
*/
1111+
@Override
1112+
protected void checkForAliasCircle(String name, String alias) {
1113+
super.checkForAliasCircle(name, alias);
1114+
if (!isAllowBeanDefinitionOverriding() && containsBeanDefinition(alias)) {
1115+
throw new IllegalStateException("Cannot register alias '" + alias +
1116+
"' for name '" + name + "': Alias would override bean definition '" + alias + "'");
1117+
}
1118+
}
1119+
11091120
@Override
11101121
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
11111122
super.registerSingleton(beanName, singletonObject);

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,6 +62,8 @@
6262
import org.springframework.core.env.ConfigurableEnvironment;
6363
import org.springframework.core.env.StandardEnvironment;
6464
import org.springframework.core.io.DescriptiveResource;
65+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
66+
import org.springframework.core.task.SyncTaskExecutor;
6567
import org.springframework.stereotype.Component;
6668
import org.springframework.util.Assert;
6769
import org.springframework.util.ObjectUtils;
@@ -375,6 +377,18 @@ public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
375377
.withMessageContaining(TestBean.class.getName());
376378
}
377379

380+
@Test // gh-25430
381+
public void detectAliasOverride() {
382+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
383+
DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
384+
beanFactory.setAllowBeanDefinitionOverriding(false);
385+
context.register(FirstConfiguration.class, SecondConfiguration.class);
386+
assertThatIllegalStateException().isThrownBy(context::refresh)
387+
.withMessageContaining("alias 'taskExecutor'")
388+
.withMessageContaining("name 'applicationTaskExecutor'")
389+
.withMessageContaining("bean definition 'taskExecutor'");
390+
}
391+
378392
@Test
379393
public void configurationClassesProcessedInCorrectOrder() {
380394
beanFactory.registerBeanDefinition("config1", new RootBeanDefinition(OverridingSingletonBeanConfig.class));
@@ -1266,6 +1280,24 @@ static class LoadedConfig {
12661280
}
12671281
}
12681282

1283+
@Configuration
1284+
static class FirstConfiguration {
1285+
1286+
@Bean
1287+
SyncTaskExecutor taskExecutor() {
1288+
return new SyncTaskExecutor();
1289+
}
1290+
}
1291+
1292+
@Configuration
1293+
static class SecondConfiguration {
1294+
1295+
@Bean(name = {"applicationTaskExecutor", "taskExecutor"})
1296+
SimpleAsyncTaskExecutor simpleAsyncTaskExecutor() {
1297+
return new SimpleAsyncTaskExecutor();
1298+
}
1299+
}
1300+
12691301
public static class ScopedProxyConsumer {
12701302

12711303
@Autowired

0 commit comments

Comments
 (0)