@@ -4634,13 +4634,28 @@ If you intend to express annotation-driven injection by name, do not primarily u
4634
4634
`@Autowired`, even if is technically capable of referring to a bean name through
4635
4635
`@Qualifier` values. Instead, use the JSR-250 `@Resource` annotation, which is
4636
4636
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.
4638
4641
4639
4642
For beans that are themselves defined as a collection/map or array type, `@Resource`
4640
4643
is a fine solution, referring to the specific collection or array bean by unique name.
4641
4644
That said, as of 4.3, collection/map and array types can be matched through Spring's
4642
4645
`@Autowired` type matching algorithm as well, as long as the element type information
4643
4646
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.
4644
4659
4645
4660
`@Autowired` applies to fields, constructors, and multi-argument methods, allowing for
4646
4661
narrowing through qualifier annotations at the parameter level. By contrast, `@Resource`
0 commit comments