Skip to content

Commit 6027be5

Browse files
committed
Ignore overridden factory methods for unique candidate resolution
See gh-27920
1 parent 455a736 commit 6027be5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private boolean isParamMismatch(Method uniqueCandidate, Method candidate) {
371371
*/
372372
private Method[] getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd) {
373373
return (mbd.isNonPublicAccessAllowed() ?
374-
ReflectionUtils.getAllDeclaredMethods(factoryClass) : factoryClass.getMethods());
374+
ReflectionUtils.getUniqueDeclaredMethods(factoryClass) : factoryClass.getMethods());
375375
}
376376

377377
private boolean isStaticCandidate(Method method, Class<?> factoryClass) {

spring-beans/src/test/java/org/springframework/beans/factory/support/ConstructorResolverAotTests.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void beanDefinitionWithFactoryMethodNameAndAssignableConstructorArg() {
8686
}
8787

8888
@Test
89-
void beanDefinitionWithFactoryMethodNameAndMatchingMethodNamesThatShouldBeIgnored() {
89+
void beanDefinitionWithFactoryMethodNameAndMatchingMethodNames() {
9090
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
9191
BeanDefinition beanDefinition = BeanDefinitionBuilder
9292
.rootBeanDefinition(DummySampleFactory.class).setFactoryMethod("of")
@@ -96,6 +96,18 @@ void beanDefinitionWithFactoryMethodNameAndMatchingMethodNamesThatShouldBeIgnore
9696
.findMethod(DummySampleFactory.class, "of", Integer.class));
9797
}
9898

99+
@Test
100+
void beanDefinitionWithFactoryMethodNameAndOverriddenMethod() {
101+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
102+
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ExtendedSampleFactory.class));
103+
BeanDefinition beanDefinition = BeanDefinitionBuilder
104+
.rootBeanDefinition(String.class).setFactoryMethodOnBean("resolve", "config")
105+
.addConstructorArgValue("test").getBeanDefinition();
106+
Executable executable = resolve(beanFactory, beanDefinition);
107+
assertThat(executable).isNotNull().isEqualTo(ReflectionUtils
108+
.findMethod(ExtendedSampleFactory.class, "resolve", String.class));
109+
}
110+
99111
@Test
100112
void detectBeanInstanceExecutableWithBeanClassAndFactoryMethodNameIgnoreTargetType() {
101113
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@@ -386,8 +398,17 @@ static String of(Integer value) {
386398
return value.toString();
387399
}
388400

389-
private String of(String ignored) {
390-
return ignored;
401+
protected String resolve(String value) {
402+
return value;
403+
}
404+
}
405+
406+
@SuppressWarnings("unused")
407+
static class ExtendedSampleFactory extends DummySampleFactory {
408+
409+
@Override
410+
protected String resolve(String value) {
411+
return super.resolve(value);
391412
}
392413
}
393414

0 commit comments

Comments
 (0)