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

@@ -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