Skip to content

Commit c6752e6

Browse files
committed
Explicit note on self references
Issue: SPR-14402
1 parent b204437 commit c6752e6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/asciidoc/core-beans.adoc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4634,13 +4634,28 @@ If you intend to express annotation-driven injection by name, do not primarily u
46344634
`@Autowired`, even if is technically capable of referring to a bean name through
46354635
`@Qualifier` values. Instead, use the JSR-250 `@Resource` annotation, which is
46364636
semantically defined to identify a specific target component by its unique name, with
4637-
the declared type being irrelevant for the matching process.
4637+
the declared type being irrelevant for the matching process. `@Autowired` has rather
4638+
different semantics: After selecting candidate beans by type, the specified String
4639+
qualifier value will be considered within those type-selected candidates only, e.g.
4640+
matching an "account" qualifier against beans marked with the same qualifier label.
46384641
46394642
For beans that are themselves defined as a collection/map or array type, `@Resource`
46404643
is a fine solution, referring to the specific collection or array bean by unique name.
46414644
That said, as of 4.3, collection/map and array types can be matched through Spring's
46424645
`@Autowired` type matching algorithm as well, as long as the element type information
46434646
is preserved in `@Bean` return type signatures or collection inheritance hierarchies.
4647+
In this case, qualifier values can be used to select among same-typed collections,
4648+
as outlined in the previous paragraph.
4649+
4650+
As of 4.3, `@Autowired` also considers self references for injection, i.e. references
4651+
back to the bean that is currently injected. Note that self injection is a fallback;
4652+
regular dependencies on other components always have precedence. In that sense, self
4653+
references do not participate in regular candidate selection and are therefore in
4654+
particular never primary; on the contrary, they always end up as lowest precedence.
4655+
In practice, use self references as a last resort only, e.g. for calling other methods
4656+
on the same instance through the bean's transactional proxy: Consider factoring out
4657+
the affected methods to a separate delegate bean in such a scenario. Alternatively,
4658+
use `@Resource` which may obtain a proxy back to the current bean by its unique name.
46444659
46454660
`@Autowired` applies to fields, constructors, and multi-argument methods, allowing for
46464661
narrowing through qualifier annotations at the parameter level. By contrast, `@Resource`

0 commit comments

Comments
 (0)