Skip to content

Commit

Permalink
fixed Overloaded @bean method causes bean to be created twice and oth…
Browse files Browse the repository at this point in the history
…er bean not to be created spring-projects#25263
  • Loading branch information
wangguogang committed Jun 30, 2020
1 parent f7dd1e3 commit 3665355
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 7 deletions.
Expand Up @@ -754,7 +754,7 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
clazz -> ReflectionUtils.getUniqueDeclaredMethods(clazz, ReflectionUtils.USER_DECLARED_METHODS));

for (Method candidate : candidates) {
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate) &&
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate,beanName) &&
candidate.getParameterCount() >= minNrOfArgs) {
// Declared type variables to inspect?
if (candidate.getTypeParameters().length > 0) {
Expand Down
Expand Up @@ -337,7 +337,7 @@ public void resolveFactoryMethodIfPossible(RootBeanDefinition mbd) {
Method[] candidates = getCandidateMethods(factoryClass, mbd);
Method uniqueCandidate = null;
for (Method candidate : candidates) {
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) {
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate,null)) {
if (uniqueCandidate == null) {
uniqueCandidate = candidate;
}
Expand Down Expand Up @@ -465,7 +465,7 @@ public BeanWrapper instantiateUsingFactoryMethod(
candidates = new ArrayList<>();
Method[] rawCandidates = getCandidateMethods(factoryClass, mbd);
for (Method candidate : rawCandidates) {
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) {
if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate,beanName)) {
candidates.add(candidate);
}
}
Expand Down
Expand Up @@ -396,7 +396,7 @@ public void setNonUniqueFactoryMethodName(String name) {
/**
* Check whether the given candidate qualifies as a factory method.
*/
public boolean isFactoryMethod(Method candidate) {
public boolean isFactoryMethod(Method candidate,String beanName) {
return candidate.getName().equals(getFactoryMethodName());
}

Expand Down
Expand Up @@ -430,8 +430,8 @@ public MethodMetadata getFactoryMethodMetadata() {
}

@Override
public boolean isFactoryMethod(Method candidate) {
return (super.isFactoryMethod(candidate) && BeanAnnotationHelper.isBeanAnnotated(candidate));
public boolean isFactoryMethod(Method candidate,String beanName) {
return (super.isFactoryMethod(candidate,beanName) && BeanAnnotationHelper.isBeanAnnotated(candidate) && (beanName == null ? true :BeanAnnotationHelper.determineBeanNameFor(candidate).equals(beanName)));
}

@Override
Expand Down
Expand Up @@ -23,8 +23,12 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.foo.SomeBean;
import org.springframework.context.annotation.foo.SomeConfig;
import org.springframework.context.annotation.foo.SomeOtherBean;
import org.springframework.util.ClassUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -64,6 +68,15 @@ public void autowiredConfigClassBeanMethodsRespectScopingWhenRegisteredViaConstr
autowiredConfigClassBeanMethodsRespectScoping(ConfigThatDoesNotImport.class, ConfigToBeAutowired.class);
}

@Test
public void checkOverloadedBean(){
ApplicationContext ctx = new AnnotationConfigApplicationContext("org.springframework.context.annotation.foo");
SomeOtherBean someOtherBean =(SomeOtherBean) ctx.getBean("other");
SomeBean someBean =(SomeBean) ctx.getBean("foo");
assertThat(someBean).isNotNull();
assertThat(someOtherBean).isNotNull();
}

private void autowiredConfigClassBeanMethodsRespectScoping(Class<?>... configClasses) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(configClasses);
Config config = ctx.getBean(Config.class);
Expand All @@ -83,7 +96,6 @@ public void importingNonConfigurationClassCausesBeanDefinitionParsingException()
}



@Configuration
static class ConfigToBeAutowired {

Expand Down
@@ -0,0 +1,9 @@
package org.springframework.context.annotation.foo;

public class SomeBean {
private final SomeOtherBean other;

public SomeBean(SomeOtherBean other) {
this.other = other;
}
}
@@ -0,0 +1,24 @@
package org.springframework.context.annotation.foo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SomeConfig {

@Bean(name = "other")
public SomeOtherBean foo() {
System.out.println("constructing SomeOtherBean");
return new SomeOtherBean();
}

@Bean(name = "foo")
public SomeBean foo(@Autowired SomeOtherBean other) {
System.out.println("constructing SomeBean");
return new SomeBean(other);
}


}
@@ -0,0 +1,6 @@
package org.springframework.context.annotation.foo;

public class SomeOtherBean {
public SomeOtherBean() {
}
}

0 comments on commit 3665355

Please sign in to comment.