Skip to content

Commit 5b9891c

Browse files
committed
Add "self injection" code example
1 parent 69d5587 commit 5b9891c

File tree

1 file changed

+50
-3
lines changed
  • framework-docs/modules/ROOT/pages/core/beans/annotation-config

1 file changed

+50
-3
lines changed

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,60 @@ regular autowiring candidate selection and are therefore in particular never pri
197197
the contrary, they always end up as lowest precedence.
198198
199199
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.
200+
calling other methods on the same instance through the bean's transactional proxy,
201+
as the following example shows:
202+
203+
[tabs]
204+
======
205+
Java::
206+
+
207+
[source,java,indent=0,subs="verbatim",role="primary"]
208+
----
209+
public class SimplePojo implements Pojo {
210+
211+
@Autowired
212+
private Pojo self;
213+
214+
public void foo() {
215+
// @Transactional on method bar() works
216+
self.bar();
217+
}
218+
219+
@Transactional
220+
public void bar() {
221+
// some logic...
222+
}
223+
}
224+
----
225+
226+
Kotlin::
227+
+
228+
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
229+
----
230+
class SimplePojo : Pojo {
231+
232+
@Autowired
233+
private lateinit var self: Pojo
234+
235+
fun foo() {
236+
// @Transactional on method bar() works
237+
self.bar()
238+
}
239+
240+
@Transactional
241+
fun bar() {
242+
// some logic...
243+
}
244+
}
245+
----
246+
======
203247
204248
Another alternative is to use `@Resource`, which may obtain a proxy back to the current
205249
bean by its unique name.
206250
251+
Another alternative is to consider factoring out the affected methods to a separate delegate
252+
bean in such a scenario.
253+
207254
======
208255
[NOTE]
209256
====

0 commit comments

Comments
 (0)