AOP之this与target区别分析
官方文档:
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
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
- this–aop对象。
- target----被代理的目标对象。
| 模式 | 描述 |
|---|---|
| this(com.aa.Father) | 当前AOP对象实现了Father接口/抽象类的某些方法,Father的父类未重写的方法不会被处理 |
| 模式 | 描述 |
|---|---|
| target(com.aa.Father) | 当前目标对象(非AOP对象)实现了Father接口/抽象类的某些方法,Father的父类未重写的方法也会被处理 |
举例
1.father类继承了grandfather中的一个say()方法,son类只是继承father,未重写say()方法
当调用father对象的say方法时,this和target都会匹配到,调用son对象的say方法时,因为未重写,this不匹配,而target会匹配到。
2.father类继承了grandfather中的一个say()方法,son类重写了say()方法.
当调用father对象的say方法时,this和target都会匹配到,调用son对象的say方法时,因为重写了,所以this和target也都会匹配到。
总结:
- 两者都可继承
- 对于目标子类,未重写的父类方法是不能被this匹配到的,可以被target匹配到
本文深入分析了在Spring AOP中,this和target关键字的区别。this限定了匹配的连接点是aop对象,即Spring的代理对象,而target则针对的是被代理的实际应用对象。通过father、son和grandfather类的示例,说明了在方法继承和重写情况下,this和target匹配的不同情况。总结:两者都能匹配继承的方法,但对于子类未重写的方法,this无法匹配,target可以。
168万+





