Skip to content

Commit 59f5c84

Browse files
committed
Improve proxying document
Self-Injection is better than horrendous `AopContext.currentProxy()`
1 parent 167cb5d commit 59f5c84

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

framework-docs/modules/ROOT/pages/core/aop/proxying.adoc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,8 @@ in the advice associated with a method invocation getting a chance to run.
193193
Okay, so what is to be done about this? The best approach (the term "best" is used
194194
loosely here) is to refactor your code such that the self-invocation does not happen.
195195
This does entail some work on your part, but it is the best, least-invasive approach.
196-
The next approach is absolutely horrendous, and we hesitate to point it out, precisely
197-
because it is so horrendous. You can (painful as it is to us) totally tie the logic
198-
within your class to Spring AOP, as the following example shows:
196+
The next approach is a little counter-intuitive, you can inject self by field or method
197+
(constructor injection is not supported), as the following example shows:
199198

200199
[tabs]
201200
======
@@ -205,9 +204,12 @@ Java::
205204
----
206205
public class SimplePojo implements Pojo {
207206
207+
@Autowired
208+
private SimplePojo self;
209+
208210
public void foo() {
209-
// this works, but... gah!
210-
((Pojo) AopContext.currentProxy()).bar();
211+
// this works
212+
self.bar();
211213
}
212214
213215
public void bar() {
@@ -222,9 +224,12 @@ Kotlin::
222224
----
223225
class SimplePojo : Pojo {
224226
227+
@Autowired
228+
private lateinit var self: SimplePojo
229+
225230
fun foo() {
226-
// this works, but... gah!
227-
(AopContext.currentProxy() as Pojo).bar()
231+
// this works
232+
self.bar()
228233
}
229234
230235
fun bar() {

0 commit comments

Comments
 (0)