Skip to content

Recommend use of self injection instead of AopContext.currentProxy() #33449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions framework-docs/modules/ROOT/pages/core/aop/proxying.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ in the advice associated with a method invocation getting a chance to run.
Okay, so what is to be done about this? The best approach (the term "best" is used
loosely here) is to refactor your code such that the self-invocation does not happen.
This does entail some work on your part, but it is the best, least-invasive approach.
The next approach is absolutely horrendous, and we hesitate to point it out, precisely
because it is so horrendous. You can (painful as it is to us) totally tie the logic
within your class to Spring AOP, as the following example shows:
The next approach is a little counter-intuitive, you can inject self by field or method
(constructor injection is not supported), as the following example shows:

[tabs]
======
Expand All @@ -205,9 +204,12 @@ Java::
----
public class SimplePojo implements Pojo {

@Autowired
private SimplePojo self;

public void foo() {
// this works, but... gah!
((Pojo) AopContext.currentProxy()).bar();
// this works
self.bar();
}

public void bar() {
Expand All @@ -222,9 +224,12 @@ Kotlin::
----
class SimplePojo : Pojo {

@Autowired
private lateinit var self: SimplePojo

fun foo() {
// this works, but... gah!
(AopContext.currentProxy() as Pojo).bar()
// this works
self.bar()
}

fun bar() {
Expand Down