Define and document rules for @Autowired self-injection in case of ambiguity [SPR-14402] #18973
Comments
Juergen Hoeller commented Self injection is indeed implemented as a kind of secondary step, with regular dependencies on other components having precedence. In that sense, self references are never primary; on the contrary, they always end up as lowest precedence. The use of field names for qualifiers is indeed debatable here. The primary reason for this not to happen is backwards compatibility: We avoid injecting a self reference where we would have cleanly injected a regular dependency before. In any case, this needs to be clearly defined and documented. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sam Brannen opened SPR-14402 and commented
Status Quo
The support for self-injection via
@Autowired
introduced in #13096 works as expected if there is a single candidate bean; however, as soon as there is more than one such candidate self-injection becomes unreliable.@Primary
and@Priority
have no influence, in contrast to standard autowiring semantics.@Qualifier
for every self-injection point.Steps to Reproduce
The following test case can be used to experiment with all of the aforementioned failure scenarios.
Running
SelfInjectionTests
as is in fact demonstrates that eachself
field consistently contains the other bean of typeUserService
.Affects: 4.3 GA
Issue Links:
@Autowired-like
self injectionReferenced from: commits c6752e6
The text was updated successfully, but these errors were encountered: