You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Define and document rules for @Autowired self-injection in case of ambiguity [SPR-14402] #18973
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.
Field names do not serve as a fallback qualifier, in contrast to standard autowiring semantics.
The only work-around appears to be the use of @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 SelfInjectionTestsas is in fact demonstrates that each self field consistently contains the other bean of type UserService.
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.
Activity
spring-projects-issues commentedon Jun 26, 2016
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.