简述
下面用两句话,和一个例子概括二者区别。
当执行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
,即注解会被子类继承的情况。
如果对你有帮助,请点个赞,或留个言激励作者继续创作😊