Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #25263 and #25350 #25342

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -442,8 +442,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,13 @@
package org.springframework.context.annotation.foo;

/**
* @author lifejwang11
* @since 5.2
*/
public class SomeBean {
private final SomeOtherBean other;

public SomeBean(SomeOtherBean other) {
this.other = other;
}
}
@@ -0,0 +1,27 @@
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;

/**
* @author lifejwang11
* @since 5.2
*/
@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,10 @@
package org.springframework.context.annotation.foo;

/**
* @author lifejwang11
* @since 5.2
*/
public class SomeOtherBean {
public SomeOtherBean() {
}
}
Expand Up @@ -377,6 +377,9 @@ else if (getType().equals(other.getType())) {
if (getSubtype().equals(other.getSubtype())) {
return true;
}
if (WILDCARD_TYPE.equals(other.getSubtype())){
return true;
}
// Wildcard with suffix? e.g. application/*+xml
if (isWildcardSubtype() || other.isWildcardSubtype()) {
int thisPlusIdx = getSubtype().lastIndexOf('+');
Expand Down
Expand Up @@ -172,6 +172,10 @@ void isCompatible() {
assertThat(suffixXml.isCompatibleWith(applicationWildcardXml)).isTrue();

assertThat(applicationWildcardXml.isCompatibleWith(MimeTypeUtils.APPLICATION_JSON)).isFalse();

MimeType applicationJSON = new MimeType("application", "vnd+json");
MimeType applicationWildcardJSON = new MimeType("application", "*");
assertThat(applicationJSON.isCompatibleWith(applicationWildcardJSON)).isTrue();
}

@Test
Expand Down