Skip to content

Commit 5529366

Browse files
committed
Reject method name mismatch in case of bean name overloading
Closes gh-33330
1 parent 599fc87 commit 5529366

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,12 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
298298
}
299299
BeanDefinition existingBeanDef = this.registry.getBeanDefinition(beanName);
300300

301-
// Is the existing bean definition one that was created from a configuration class?
302-
// -> allow the current bean method to override, since both are at second-pass level.
303-
// However, if the bean method is an overloaded case on the same configuration class,
304-
// preserve the existing bean definition.
301+
// If the bean method is an overloaded case on the same configuration class,
302+
// preserve the existing bean definition and mark it as overloaded.
305303
if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
306-
if (ccbd.getMetadata().getClassName().equals(
307-
beanMethod.getConfigurationClass().getMetadata().getClassName())) {
308-
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(ccbd.getFactoryMethodName())) {
309-
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
310-
}
304+
if (ccbd.getMetadata().getClassName().equals(beanMethod.getConfigurationClass().getMetadata().getClassName()) &&
305+
ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
306+
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
311307
return true;
312308
}
313309
else {

spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.beans.factory.config.DependencyDescriptor;
4242
import org.springframework.beans.factory.config.ListFactoryBean;
4343
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
44+
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
4445
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4546
import org.springframework.beans.factory.support.RootBeanDefinition;
4647
import org.springframework.beans.testfixture.beans.ITestBean;
@@ -219,6 +220,12 @@ void configurationWithNullReference() {
219220
assertThat(foo.getSpouse()).isNull();
220221
}
221222

223+
@Test // gh-33330
224+
void configurationWithMethodNameMismatch() {
225+
assertThatExceptionOfType(BeanDefinitionOverrideException.class)
226+
.isThrownBy(() -> initBeanFactory(ConfigWithMethodNameMismatch.class));
227+
}
228+
222229
@Test
223230
void configurationWithAdaptivePrototypes() {
224231
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
@@ -352,6 +359,7 @@ private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
352359
String configBeanName = configClass.getName();
353360
factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
354361
}
362+
factory.setAllowBeanDefinitionOverriding(false);
355363
ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
356364
ccpp.postProcessBeanDefinitionRegistry(factory);
357365
ccpp.postProcessBeanFactory(factory);
@@ -526,6 +534,19 @@ public TestBean bar() {
526534
}
527535

528536

537+
@Configuration
538+
static class ConfigWithMethodNameMismatch {
539+
540+
@Bean(name = "foo") public TestBean foo() {
541+
return new SpousyTestBean("foo");
542+
}
543+
544+
@Bean(name = "foo") public TestBean fooX() {
545+
return new SpousyTestBean("fooX");
546+
}
547+
}
548+
549+
529550
@Scope("prototype")
530551
static class AdaptiveInjectionPoints {
531552

0 commit comments

Comments
 (0)