通俗易懂说明@within和@target区别--SpringAOP

通俗易懂说明@within和@target区别--SpringAOP

简述

下面用两句话,和一个例子概括二者区别。

当执行son.f()时,
@target:会去查看使用方法f()的对象son的类S是否有注解A
@within:会去查看方法f()属于的类P是否有注解A
一个是使用的对象,一个是所属的类。

eg:

@A
class P{
    f();
}
class S{
}
test(){
    S son = new S();
    son.f();
}

详细

下面搬出官方文档并进行说明:
@target: Limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type.

@within: Limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP).
注意两处不同,@target说的是where the class of the executing object has an annotation of the given type.,说的是执行想要切入的方法(切点)的对象的类具有给定类型的注释。@within说的是the execution of methods declared in types with the given annotation,可以理解为切点所在的类有注解。

基于此原理,在使用时就会表现为:
@within(A)切入的是被@A注解的类的所有方法中,没有被子类重写的方法(重写后调用该方法时,该方法所在类就是没@A的子类了)。

需要注意的是,A注解标注了@Inherited,即注解会被子类继承的情况。


如果对你有帮助,请点个赞,或留个言激励作者继续创作😊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值