Skip to content

Commit 22d2810

Browse files
committed
Narrow method annotation check in hasQualifier to setter methods
Closes gh-35908 (cherry picked from commit 6c3132c)
1 parent b39055f commit 22d2810

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,12 @@ public boolean hasQualifier(DependencyDescriptor descriptor) {
376376
}
377377
MethodParameter methodParam = descriptor.getMethodParameter();
378378
if (methodParam != null) {
379-
for (Annotation annotation : methodParam.getMethodAnnotations()) {
380-
if (isQualifier(annotation.annotationType())) {
381-
return true;
379+
Method method = methodParam.getMethod();
380+
if (method == null || void.class == method.getReturnType()) {
381+
for (Annotation annotation : methodParam.getMethodAnnotations()) {
382+
if (isQualifier(annotation.annotationType())) {
383+
return true;
384+
}
382385
}
383386
}
384387
}

spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
22+
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Optional;
2425

@@ -29,6 +30,7 @@
2930
import org.springframework.beans.factory.InitializingBean;
3031
import org.springframework.beans.factory.ObjectFactory;
3132
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.beans.factory.annotation.Qualifier;
3234
import org.springframework.beans.factory.annotation.Value;
3335
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
3436
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -100,6 +102,16 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
100102
context.close();
101103
}
102104

105+
@Test
106+
void testAutowiredConfigurationMethodDependenciesWithQualifier() {
107+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
108+
QualifiedAutowiredMethodConfig.class);
109+
110+
assertThat(context.getBeansOfType(Colour.class)).isEmpty();
111+
assertThat(context.getBean(TestBean.class).getName()).isEmpty();
112+
context.close();
113+
}
114+
103115
@Test
104116
void testAutowiredSingleConstructorSupported() {
105117
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
@@ -297,6 +309,25 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours
297309
}
298310

299311

312+
@Configuration
313+
static class QualifiedAutowiredMethodConfig {
314+
315+
@Bean
316+
@Qualifier("testBean")
317+
public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) {
318+
if (!colour.isEmpty() || !colours.isEmpty()) {
319+
throw new IllegalStateException("Unexpected match: " + colour + " " + colours);
320+
}
321+
return new TestBean("");
322+
}
323+
324+
@Bean
325+
public List<?> someList() {
326+
return Collections.singletonList(new TestBean("shouldNotMatch"));
327+
}
328+
}
329+
330+
300331
@Configuration
301332
static class AutowiredConstructorConfig {
302333

0 commit comments

Comments
 (0)