Skip to content

Commit

Permalink
Reject method name mismatch in case of bean name overloading
Browse files Browse the repository at this point in the history
Closes gh-33330
  • Loading branch information
jhoeller committed Aug 13, 2024
1 parent 599fc87 commit 5529366
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,12 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
}
BeanDefinition existingBeanDef = this.registry.getBeanDefinition(beanName);

// Is the existing bean definition one that was created from a configuration class?
// -> allow the current bean method to override, since both are at second-pass level.
// However, if the bean method is an overloaded case on the same configuration class,
// preserve the existing bean definition.
// If the bean method is an overloaded case on the same configuration class,
// preserve the existing bean definition and mark it as overloaded.
if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
if (ccbd.getMetadata().getClassName().equals(
beanMethod.getConfigurationClass().getMetadata().getClassName())) {
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(ccbd.getFactoryMethodName())) {
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
}
if (ccbd.getMetadata().getClassName().equals(beanMethod.getConfigurationClass().getMetadata().getClassName()) &&
ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
return true;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.config.ListFactoryBean;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.ITestBean;
Expand Down Expand Up @@ -219,6 +220,12 @@ void configurationWithNullReference() {
assertThat(foo.getSpouse()).isNull();
}

@Test // gh-33330
void configurationWithMethodNameMismatch() {
assertThatExceptionOfType(BeanDefinitionOverrideException.class)
.isThrownBy(() -> initBeanFactory(ConfigWithMethodNameMismatch.class));
}

@Test
void configurationWithAdaptivePrototypes() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
Expand Down Expand Up @@ -352,6 +359,7 @@ private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
String configBeanName = configClass.getName();
factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
}
factory.setAllowBeanDefinitionOverriding(false);
ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
ccpp.postProcessBeanDefinitionRegistry(factory);
ccpp.postProcessBeanFactory(factory);
Expand Down Expand Up @@ -526,6 +534,19 @@ public TestBean bar() {
}


@Configuration
static class ConfigWithMethodNameMismatch {

@Bean(name = "foo") public TestBean foo() {
return new SpousyTestBean("foo");
}

@Bean(name = "foo") public TestBean fooX() {
return new SpousyTestBean("fooX");
}
}


@Scope("prototype")
static class AdaptiveInjectionPoints {

Expand Down

0 comments on commit 5529366

Please sign in to comment.