spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.75k stars 38.16k forks source link

Bean with `defaultCandidate = false` and qualifier does not work with constructor injection #33762

Closed nosan closed 1 month ago

nosan commented 1 month ago

While working on PoC for https://github.com/spring-projects/spring-boot/issues/42746, I encountered a strange behaviour when a bean is qualified and has default-candidate = false. For some reason, injection does not work when the constructor has a qualified parameter.

private final SomeService someService;

AutowireByConstructorQualifier(@Qualifier("test") SomeService someService) {
    this.someService = someService;
}

As I understood, default-candidate should back off the injection by plain type, but it should work when the parameter has a @Qualifier annotation. What is weird that the following syntax works fine:

@Qualifier("test")
@Autowired
public void setSomeService(@Qualifier("test") SomeService someService) {
    this.someService = someService;
}

It seems like it might be a bug, but I'm not entirely sure.

Sample: default-candidate-qualifier.zip

nosan commented 1 month ago

By the way, this set method does not work as well:


@Autowired
public void setSomeService(@Qualifier("test") SomeService someService) {
    this.someService = someService;
}
nosan commented 1 month ago

Thank you, @jhoeller