Baeldung has this section:
this limits matching to join points where the bean reference is an instance of the given type, while target limits matching to join points where the target object is an instance of the given type. The former works when Spring AOP creates a CGLIB-based proxy, and the latter is used when a JDK-based proxy is created. Suppose that the target class implements an interface:
public class FooDao implements BarDao {
...
}
In this case, Spring AOP will use the JDK-based proxy, and we should use the target PCD because the proxied object will be an instance of the Proxy class and implement the BarDao interface:
@Pointcut("target(com.baeldung.pointcutadvice.dao.BarDao)")
On the other hand, if FooDao doesn't implement any interface, or the proxyTargetClass property is set to true, then the proxied object will be a subclass of FooDao and we can use the this PCD:
@Pointcut("this(com.baeldung.pointcutadvice.dao.FooDao)")
I'm still confuse why this just works with CGLIB proxy and target just works with JDK proxy. Could you help to tell me the different between them?
Actually, the explanation in the tutorial does not make much sense:
this(MyInterface)andtarget(MyInterface)work for JDK proxies, if the bean is declared as aMyInterfacetype.this(MyClass)andtarget(MyClass)work for CGLIB proxies, if the bean is declared as aMyClasstype.Just try, and you will see that I am right. In Spring AOP, there is not real difference between
this()andtarget(), because due to its proxy-based nature, it implicitly only supportsexecution()pointcuts from AspectJ.In native AspectJ however, you also have other pointcut types such as
call(), and there you would see a difference:this()would match the caller's type, whiletarget()would match the callee's type. E.g., if you intercept a methodA.a()callingB.b()via pointcutcall(B.b()),this()would return anAinstance, whiletarget()would return aBinstance. Do not worry, if this is difficult to understand for you, because for Spring AOP and execution pointcuts it really does not matter.The only subtle difference I noticed in Spring AOP is, that for
MyInterfaceImpl implements MyInterface, pointcuttarget(MyInterfaceImpl)would actually match, whilethis(MyInterfaceImpl)would not. This is because for JDK proxies, the proxy actually extendsjava.lang.reflect.Proxy, notMyInterfaceImpl. The proxy only delegates to aMyInterfaceImplinstance.Edit: If you keep in mind that, in contrast to native AspectJ which involves no dynamic proxies, the semantics in Spring AOP are such that
this()relates to the proxy object, whiletarget()relates to the proxied object (proxy target),it becomes clear why in this special case for JDK proxies
target()matches, butthis()does not.Reference: Spring manual, section "Declaring a pointcut - examples":
Only in this case, we are not asking for the interface class (which both the proxy and the target object implement), but for the implementation class itself.
Bottom line: For all intents and purposes, in normal use cases you can use either
this()ortarget()for both JDK and CGLIB proxies. I recommend to stick withtarget(), because it best matches the implicitexecution()semantics of Spring AOP, and usually you are interested in information about the target rather than about the proxy..