Skip to content

Commit 5e1de19

Browse files
committed
Consolidate self injection documentation in the reference manual
Closes gh-33454
1 parent a19acc6 commit 5e1de19

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,10 @@ is preserved in `@Bean` return type signatures or collection inheritance hierarc
179179
In this case, you can use qualifier values to select among same-typed collections,
180180
as outlined in the previous paragraph.
181181

182-
`@Autowired` also considers self references for injection (that is, references
183-
back to the bean that is currently injected). Note that self injection is a fallback.
184-
Regular dependencies on other components always have precedence. In that sense, self
185-
references do not participate in regular candidate selection and are therefore in
186-
particular never primary. On the contrary, they always end up as lowest precedence.
187-
In practice, you should use self references as a last resort only (for example, for
188-
calling other methods on the same instance through the bean's transactional proxy).
189-
Consider factoring out the affected methods to a separate delegate bean in such a scenario.
190-
Alternatively, you can use `@Resource`, which may obtain a proxy back to the current bean
191-
by its unique name.
192-
193-
[NOTE]
194-
====
195-
Trying to inject the results from `@Bean` methods on the same configuration class is
196-
effectively a self-reference scenario as well. Either lazily resolve such references
197-
in the method signature where it is actually needed (as opposed to an autowired field
198-
in the configuration class) or declare the affected `@Bean` methods as `static`,
199-
decoupling them from the containing configuration class instance and its lifecycle.
200-
Otherwise, such beans are only considered in the fallback phase, with matching beans
201-
on other configuration classes selected as primary candidates instead (if available).
202-
====
182+
`@Autowired` also considers self references for injection (that is, references back to
183+
the bean that is currently injected). See
184+
xref:core/beans/annotation-config/autowired.adoc#beans-autowired-annotation-self-injection[Self Injection]
185+
for details.
203186

204187
`@Autowired` applies to fields, constructors, and multi-argument methods, allowing for
205188
narrowing through qualifier annotations at the parameter level. In contrast, `@Resource`

framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,37 @@ implementation type, consider declaring the most specific return type on your fa
186186
method (at least as specific as required by the injection points referring to your bean).
187187
====
188188

189+
.[[beans-autowired-annotation-self-injection]]Self Injection
190+
****
191+
`@Autowired` also considers self references for injection (that is, references back to
192+
the bean that is currently injected).
193+
194+
Note, however, that self injection is a fallback mechanism. Regular dependencies on other
195+
components always have precedence. In that sense, self references do not participate in
196+
regular autowiring candidate selection and are therefore in particular never primary. On
197+
the contrary, they always end up as lowest precedence.
198+
199+
In practice, you should use self references as a last resort only – for example, for
200+
calling other methods on the same instance through the bean's transactional proxy. As an
201+
alternative, consider factoring out the affected methods to a separate delegate bean in
202+
such a scenario.
203+
204+
Another alternative is to use `@Resource`, which may obtain a proxy back to the current
205+
bean by its unique name.
206+
207+
======
189208
[NOTE]
190209
====
191-
As of 4.3, `@Autowired` also considers self references for injection (that is, references
192-
back to the bean that is currently injected). Note that self injection is a fallback.
193-
In practice, you should use self references as a last resort only (for example, for
194-
calling other methods on the same instance through the bean's transactional proxy).
195-
Consider factoring out the affected methods to a separate delegate bean in such a scenario.
210+
Trying to inject the results from `@Bean` methods in the same `@Configuration` class is
211+
effectively a self-reference scenario as well. Either lazily resolve such references
212+
in the method signature where it is actually needed (as opposed to an autowired field
213+
in the configuration class) or declare the affected `@Bean` methods as `static`,
214+
decoupling them from the containing configuration class instance and its lifecycle.
215+
Otherwise, such beans are only considered in the fallback phase, with matching beans
216+
on other configuration classes selected as primary candidates instead (if available).
196217
====
218+
======
219+
****
197220

198221
You can also instruct Spring to provide all beans of a particular type from the
199222
`ApplicationContext` by adding the `@Autowired` annotation to a field or method that

0 commit comments

Comments
 (0)