Narrow method annotation check in hasQualifier to setter methods

Closes gh-35908

(cherry picked from commit 6c3132cb8c)
This commit is contained in:
Juergen Hoeller
2025-11-26 23:01:55 +01:00
parent b39055f293
commit 22d2810ed0
2 changed files with 37 additions and 3 deletions
@@ -376,9 +376,12 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
}
MethodParameter methodParam = descriptor.getMethodParameter();
if (methodParam != null) {
for (Annotation annotation : methodParam.getMethodAnnotations()) {
if (isQualifier(annotation.annotationType())) {
return true;
Method method = methodParam.getMethod();
if (method == null || void.class == method.getReturnType()) {
for (Annotation annotation : methodParam.getMethodAnnotations()) {
if (isQualifier(annotation.annotationType())) {
return true;
}
}
}
}
@@ -19,6 +19,7 @@ package org.springframework.context.annotation.configuration;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -29,6 +30,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -100,6 +102,16 @@ class AutowiredConfigurationTests {
context.close();
}
@Test
void testAutowiredConfigurationMethodDependenciesWithQualifier() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
QualifiedAutowiredMethodConfig.class);
assertThat(context.getBeansOfType(Colour.class)).isEmpty();
assertThat(context.getBean(TestBean.class).getName()).isEmpty();
context.close();
}
@Test
void testAutowiredSingleConstructorSupported() {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
@@ -297,6 +309,25 @@ class AutowiredConfigurationTests {
}
@Configuration
static class QualifiedAutowiredMethodConfig {
@Bean
@Qualifier("testBean")
public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) {
if (!colour.isEmpty() || !colours.isEmpty()) {
throw new IllegalStateException("Unexpected match: " + colour + " " + colours);
}
return new TestBean("");
}
@Bean
public List<?> someList() {
return Collections.singletonList(new TestBean("shouldNotMatch"));
}
}
@Configuration
static class AutowiredConstructorConfig {