Can somebody explain this & target pointcut design

2020-06-20 16:35发布

问题:

I am new to Spring AOP and was reading the docs for pointcut designators. Both this and target designators sound same to me. Can someone explain with a better/cleaner example? Thanks

this - limits matching to join points (the execution of methods when using Spring AOP) where the bean reference (Spring AOP proxy) is an instance of the given type

eg: this(com.xyz.service.AccountService)

any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:

target - limits matching to join points (the execution of methods when using Spring AOP) where the target object (application object being proxied) is an instance of the given type

eg: target(com.xyz.service.AccountService)

any join point (method execution only in Spring AOP) where the target object implements the AccountService interface

Link : http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html

回答1:

The different proxying methods available, JDK and CGLIB, allow you to add more types to your object than those that it inherits. For example, you can declare a Foo bean that extends no classes (except Object) and implements no interfaces. For whatever reason, you can decide that you want to proxy this bean and make it implement the Bar interface and extend the SomeRandomType class. The target object here would be the bean of type Foo. The Spring proxy is an object that delegates to the target bean, shares its type, and additionally can have more types, as in the example above.

Therefore target refers to the proxied bean and this refers to the proxy.