@@ -5092,6 +5092,7 @@ If there is no other resolution indicator (such as a qualifier or a primary mark
5092
5092
for a non-unique dependency situation, Spring matches the injection point name
5093
5093
(that is, the field name or parameter name) against the target bean names and choose the
5094
5094
same-named candidate, if any.
5095
+ ====
5095
5096
5096
5097
That said, if you intend to express annotation-driven injection by name, do not
5097
5098
primarily use `@Autowired`, even if it is capable of selecting by bean name among
@@ -5115,17 +5116,28 @@ back to the bean that is currently injected). Note that self injection is a fall
5115
5116
Regular dependencies on other components always have precedence. In that sense, self
5116
5117
references do not participate in regular candidate selection and are therefore in
5117
5118
particular never primary. On the contrary, they always end up as lowest precedence.
5118
- In practice, you should use self references as a last resort only (for example, for calling other methods
5119
- on the same instance through the bean's transactional proxy). Consider factoring out
5120
- the effected methods to a separate delegate bean in such a scenario. Alternatively, you
5121
- can use `@Resource`, which may obtain a proxy back to the current bean by its unique name.
5119
+ In practice, you should use self references as a last resort only (for example, for
5120
+ calling other methods on the same instance through the bean's transactional proxy).
5121
+ Consider factoring out the effected methods to a separate delegate bean in such a scenario.
5122
+ Alternatively, you can use `@Resource`, which may obtain a proxy back to the current bean
5123
+ by its unique name.
5124
+
5125
+ [NOTE]
5126
+ ====
5127
+ Trying to inject the results from `@Bean` methods on the same configuration class is
5128
+ effectively a self-reference scenario as well. Either lazily resolve such references
5129
+ in the method signature where it is actually needed (as opposed to an autowired field
5130
+ in the configuration class) or declare the affected `@Bean` methods as `static`,
5131
+ decoupling them from the containing configuration class instance and its lifecycle.
5132
+ Otherwise, such beans are only considered in the fallback phase, with matching beans
5133
+ on other configuration classes selected as primary candidates instead (if available).
5134
+ ====
5122
5135
5123
5136
`@Autowired` applies to fields, constructors, and multi-argument methods, allowing for
5124
- narrowing through qualifier annotations at the parameter level. By contrast, `@Resource`
5137
+ narrowing through qualifier annotations at the parameter level. In contrast, `@Resource`
5125
5138
is supported only for fields and bean property setter methods with a single argument.
5126
- As a consequence, you should stick with qualifiers if your injection target is a constructor or a
5127
- multi-argument method.
5128
- ====
5139
+ As a consequence, you should stick with qualifiers if your injection target is a
5140
+ constructor or a multi-argument method.
5129
5141
5130
5142
You can create your own custom qualifier annotations. To do so, define an annotation and
5131
5143
provide the `@Qualifier` annotation within your definition, as the following example shows:
0 commit comments