AOP之this与target区别分析

本文深入分析了在Spring AOP中,this和target关键字的区别。this限定了匹配的连接点是aop对象,即Spring的代理对象,而target则针对的是被代理的实际应用对象。通过father、son和grandfather类的示例,说明了在方法继承和重写情况下,this和target匹配的不同情况。总结:两者都能匹配继承的方法,但对于子类未重写的方法,this无法匹配,target可以。

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匹配到
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值