From Spring Documentation:
any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:
this(com.xyz.service.AccountService)
any join point (method execution only in Spring AOP) where the target object implements the AccountService interface:
target(com.xyz.service.AccountService)
I don't understand what "target object" and the expression target(...)
mean.
How is target
different from this
?
Target Object: These are the objects on which advices are applied. In Spring AOP, a subclass is created at runtime where the target method is overridden and advices are included based on their configuration. Proxy: It is an object that is created after applying advice to the target object.
Executing method on the target class Thus, Spring AOP injects a proxy instead of an actual instance of the target class. When we start the Spring or Spring Boot application, we see Spring executes the advice before the actual method.
JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.
This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")
this(AType)
means all join points where this instanceof AType
is true. So this means that in your case once the call reaches any method of AccountService this instanceof AccountService
will be true.
target(AType)
means all join points where anObject instanceof AType
. If you are calling a method on an object and that object is an instanceof AccountService, that will be a valid joinpoint.
To summarize a different way - this(AType)
is from a receivers perspective, and target(AType)
is from a callers perspective.
I know this is an old post but I just came across an important difference between this and target while not using AspectJ.
Consider the following introduction aspect:
@Aspect public class IntroductionsAspect { @DeclareParents(value="a.b.c.D", defaultImpl=XImpl.class) public static X x; @After("execution(* a.b.c.D.*(..)) && this(traceable)") public void x(Traceable traceable) { traceable.increment(); } }
Simply put, this aspect is doing two things:
a.b.c.D
class implement the X
interface.traceable.increment()
to be executed before each method of a.b.c.D
.The important part is "execution(* a.b.c.D.*(..)) && this(traceable)"
. Notice that I used this, not target.
If you use target instead, you are trying to match the original class a.b.c.D
, not the introduced interface X
. So Spring AOP will not find any join point in a.b.c.D
.
In summary:
this - Checks the proxy type, or introduced type. target - Checks the declared type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With