Skip to content

Commit

Permalink
Consistent behavior for overloaded @bean methods with ASM processing
Browse files Browse the repository at this point in the history
Closes gh-25263
  • Loading branch information
jhoeller committed Aug 27, 2020
1 parent f43c39c commit 60fa704
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Expand Up @@ -211,7 +211,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
return;
}

ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata, beanName);
beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));

if (metadata.isStatic()) {
Expand Down Expand Up @@ -276,7 +276,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
new BeanDefinitionHolder(beanDef, beanName), this.registry,
proxyMode == ScopedProxyMode.TARGET_CLASS);
beanDefToRegister = new ConfigurationClassBeanDefinition(
(RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata);
(RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata, beanName);
}

if (logger.isTraceEnabled()) {
Expand Down Expand Up @@ -398,24 +398,31 @@ private static class ConfigurationClassBeanDefinition extends RootBeanDefinition

private final MethodMetadata factoryMethodMetadata;

public ConfigurationClassBeanDefinition(ConfigurationClass configClass, MethodMetadata beanMethodMetadata) {
private final String derivedBeanName;

public ConfigurationClassBeanDefinition(
ConfigurationClass configClass, MethodMetadata beanMethodMetadata, String derivedBeanName) {

this.annotationMetadata = configClass.getMetadata();
this.factoryMethodMetadata = beanMethodMetadata;
this.derivedBeanName = derivedBeanName;
setResource(configClass.getResource());
setLenientConstructorResolution(false);
}

public ConfigurationClassBeanDefinition(
RootBeanDefinition original, ConfigurationClass configClass, MethodMetadata beanMethodMetadata) {
public ConfigurationClassBeanDefinition(RootBeanDefinition original,
ConfigurationClass configClass, MethodMetadata beanMethodMetadata, String derivedBeanName) {
super(original);
this.annotationMetadata = configClass.getMetadata();
this.factoryMethodMetadata = beanMethodMetadata;
this.derivedBeanName = derivedBeanName;
}

private ConfigurationClassBeanDefinition(ConfigurationClassBeanDefinition original) {
super(original);
this.annotationMetadata = original.annotationMetadata;
this.factoryMethodMetadata = original.factoryMethodMetadata;
this.derivedBeanName = original.derivedBeanName;
}

@Override
Expand All @@ -431,7 +438,8 @@ public MethodMetadata getFactoryMethodMetadata() {

@Override
public boolean isFactoryMethod(Method candidate) {
return (super.isFactoryMethod(candidate) && BeanAnnotationHelper.isBeanAnnotated(candidate));
return (super.isFactoryMethod(candidate) && BeanAnnotationHelper.isBeanAnnotated(candidate) &&
BeanAnnotationHelper.determineBeanNameFor(candidate).equals(this.derivedBeanName));
}

@Override
Expand Down
@@ -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.
Expand Down Expand Up @@ -280,12 +280,32 @@ public void configurationWithApplicationListener() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithApplicationListener.class);
ctx.refresh();

ConfigWithApplicationListener config = ctx.getBean(ConfigWithApplicationListener.class);
assertThat(config.closed).isFalse();
ctx.close();
assertThat(config.closed).isTrue();
}

@Test
public void configurationWithOverloadedBeanMismatch() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.registerBeanDefinition("config", new RootBeanDefinition(OverloadedBeanMismatch.class));
ctx.refresh();

TestBean tb = ctx.getBean(TestBean.class);
assertThat(tb.getLawyer()).isEqualTo(ctx.getBean(NestedTestBean.class));
}

@Test
public void configurationWithOverloadedBeanMismatchWithAsm() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.registerBeanDefinition("config", new RootBeanDefinition(OverloadedBeanMismatch.class.getName()));
ctx.refresh();

TestBean tb = ctx.getBean(TestBean.class);
assertThat(tb.getLawyer()).isEqualTo(ctx.getBean(NestedTestBean.class));
}


/**
Expand Down Expand Up @@ -595,4 +615,21 @@ public ApplicationListener<ContextClosedEvent> listener() {
}
}


@Configuration
public static class OverloadedBeanMismatch {

@Bean(name = "other")
public NestedTestBean foo() {
return new NestedTestBean();
}

@Bean(name = "foo")
public TestBean foo(@Qualifier("other") NestedTestBean other) {
TestBean tb = new TestBean();
tb.setLawyer(other);
return tb;
}
}

}

0 comments on commit 60fa704

Please sign in to comment.