Can anyone explain the difference between this()
and target()
pointcuts in aspectj. I tried finding this elsewhere but there doesnt seem to be a clear cut answer. Thank You
AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)
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.
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.
AspectJ supports three kinds of advice. The kind of advice determines how it interacts with the join points it is defined over. Thus AspectJ divides advice into that which runs before its join points, that which runs after its join points, and that which runs in place of (or "around") its join points.
At a matching join point, this()
is the object you are in, target()
is the object you are invoking/referencing. The confusion may arise because in the case of an execution()
pointcut matching on a joint point they are the same thing - the object containing the execution join point which matched is the same as the object running the method you are matching on. But in the case of a call()
join point they are different. The object making the call is different from the object on which the method is being called.
class A {
public void m() {
B b = new B();
b.n();
}
}
class B {
public void n() {
}
}
For that setup, the pointcut execution(* m(..))
will match on join point A.m()
and have a this()
of type A and target()
of type A (and they will be the same instance of A). However the pointcut call(* n(..))
will match at the call site in method A.m()
where it calls n()
and at that point this()
will be the instance of A making the call whilst target()
will be the instance of B that the method is being invoked upon.
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