Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP target() vs this()

Tags:

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?

like image 885
rapt Avatar asked Aug 12 '12 18:08

rapt


People also ask

What is target object in spring AOP?

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.

Which method is executed first in AOP?

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.

What is pointcut and JoinPoint in spring?

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.

What methods does this pointcut expression reference @target?

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,..))")


2 Answers

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.

like image 65
Biju Kunjummen Avatar answered Nov 12 '22 09:11

Biju Kunjummen


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:

  1. Making the a.b.c.D class implement the X interface.
  2. Adding a call to 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.

like image 43
Akira Avatar answered Nov 12 '22 07:11

Akira