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

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

Closed
nosan opened this issue Oct 21, 2024 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@nosan
Copy link
Contributor

nosan commented Oct 21, 2024

While working on PoC for spring-projects/spring-boot#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 nosan changed the title Bean with default-candidate=false and qualifier does work with constructor injection. Bean with default-candidate=false and qualifier does not work with constructor injection. Oct 21, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 21, 2024
@nosan
Copy link
Contributor Author

nosan commented Oct 21, 2024

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

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

@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Oct 21, 2024
@jhoeller jhoeller self-assigned this Oct 21, 2024
@sbrannen sbrannen changed the title Bean with default-candidate=false and qualifier does not work with constructor injection. Bean with defaultCandidate = false and qualifier does not work with constructor injection Oct 21, 2024
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 21, 2024
@jhoeller jhoeller added this to the 6.2.0 milestone Oct 21, 2024
@nosan
Copy link
Contributor Author

nosan commented Oct 21, 2024

Thank you, @jhoeller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants