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