@target 和@within 的区别 (Spring AOP)
Difference between @target and @within (Spring AOP)
Spring 手册说:
any join point (method execution only in Spring AOP) where the target
object has an @Transactional annotation:
@target(org.springframework.transaction.annotation .Transactional)
any join point (method execution only in Spring AOP) where the
declared type of the target object has an @Transactional annotation:
@within(org.springframework.transaction.annotation .Transactional)
但我看不出它们有什么区别!
我试过 Google 它:
One difference between the two is that @within() is matched statically, requiring the corresponding annotation type to have only
the CLASS retention. Whereas, @target() is matched at runtime,
requiring the same to have the RUNTIME retention. Other than that,
within the context of Spring, here is no difference between the join
points selected by two.
所以我尝试添加带有 CLASS 保留的自定义注释,但是 Spring 抛出异常(因为注释 必须 运行时间保留)
您引用的信息是正确的,但是 只有 @target
个切入点指示符需要带有 RUNTIME
保留的注释,而 @within
只需要 CLASS
保留。
让我们考虑以下两个简单的注释:
ClassRetAnnotation.java
package mypackage;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.CLASS)
public @interface ClassRetAnnotation {}
RuntimeRetAnnotation.java
package mypackage;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RuntimeRetAnnotation {}
现在,如果你像下面这样定义一个方面,运行时不会有异常:
@Component
@Aspect
public class MyAspect {
@Before("@within(mypackage.ClassRetAnnotation)")
public void within() { System.out.println("within"); }
@Before("@target(mypackage.RuntimeRetAnnotation)")
public void target() { System.out.println("target"); }
}
我希望这个例子有助于阐明您指出的细微差别。
Spring参考:https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/core.html#aop-pointcuts
您没有注意到任何区别,因为 Spring AOP 在使用 AspectJ 语法时实际上只模拟了其功能的有限子集。因为SpringAOP是基于动态代理的,所以只提供public的拦截,非静态方法执行。 (当使用 CGLIB 代理时,您还可以拦截包范围和受保护的方法。)但是 AspectJ 还可以拦截方法调用(不仅仅是执行)、成员字段访问(静态和非静态)、构造函数 call/execution、静态 class 初始化等等。
那么让我们构建一个非常简单的 AspectJ 示例:
标记注释:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
驱动申请:
package de.scrum_master.app;
@MyAnnotation
public class Application {
private int nonStaticMember;
private static int staticMember;
public void doSomething() {
System.out.println("Doing something");
nonStaticMember = 11;
}
public void doSomethingElse() {
System.out.println("Doing something else");
staticMember = 22;
}
public static void main(String[] args) {
Application application = new Application();
application.doSomething();
application.doSomethingElse();
}
}
看点:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("@within(de.scrum_master.app.MyAnnotation) && execution(public !static * *(..))")
public void adviceAtWithin(JoinPoint thisJoinPoint) {
System.out.println("[@within] " + thisJoinPoint);
}
@Before("@target(de.scrum_master.app.MyAnnotation) && execution(public !static * *(..))")
public void adviceAtTarget(JoinPoint thisJoinPoint) {
System.out.println("[@target] " + thisJoinPoint);
}
}
请注意,我在这里通过向两个切入点添加 && execution(public !static * *(..))
来模拟 Spring AOP 行为。
控制台日志:
[@within] execution(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
Doing something
[@within] execution(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
Doing something else
这并不奇怪。这正是您在 Spring AOP 中也会看到的内容。现在,如果您从两个切入点中删除 && execution(public !static * *(..))
部分,在 Spring AOP 中,输出仍然相同,但在 AspectJ 中(例如,如果您在 Spring 中激活 AspectJ LTW),它会更改为:
[@within] staticinitialization(de.scrum_master.app.Application.<clinit>)
[@within] execution(void de.scrum_master.app.Application.main(String[]))
[@within] call(de.scrum_master.app.Application())
[@within] preinitialization(de.scrum_master.app.Application())
[@within] initialization(de.scrum_master.app.Application())
[@target] initialization(de.scrum_master.app.Application())
[@within] execution(de.scrum_master.app.Application())
[@target] execution(de.scrum_master.app.Application())
[@within] call(void de.scrum_master.app.Application.doSomething())
[@target] call(void de.scrum_master.app.Application.doSomething())
[@within] execution(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something
[@within] set(int de.scrum_master.app.Application.nonStaticMember)
[@target] set(int de.scrum_master.app.Application.nonStaticMember)
[@within] call(void de.scrum_master.app.Application.doSomethingElse())
[@target] call(void de.scrum_master.app.Application.doSomethingElse())
[@within] execution(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something else
[@within] set(int de.scrum_master.app.Application.staticMember)
当详细查看时,您会发现更多 @within()
个连接点被拦截,但也有更多 @target()
个连接点,例如call()
前面提到的连接点,还有 set()
用于非静态字段和对象 initialization()
在构造函数执行之前发生。
仅查看 @target()
时,我们会看到:
[@target] initialization(de.scrum_master.app.Application())
[@target] execution(de.scrum_master.app.Application())
[@target] call(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
Doing something
[@target] set(int de.scrum_master.app.Application.nonStaticMember)
[@target] call(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
Doing something else
对于这些方面输出行中的每一个,我们还会看到相应的 @within()
匹配项。现在让我们关注不一样的地方,过滤输出的差异:
[@within] staticinitialization(de.scrum_master.app.Application.<clinit>)
[@within] execution(void de.scrum_master.app.Application.main(String[]))
[@within] call(de.scrum_master.app.Application())
[@within] preinitialization(de.scrum_master.app.Application())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something else
[@within] set(int de.scrum_master.app.Application.staticMember)
这里你看,按顺序排列
- 静态class初始化,
- 静态方法执行,
- 构造函数调用(尚未执行!),
- 构造对象预初始化,
- 另一个class(
System.out
)中的成员变量访问,
- 从另一个调用方法 class (
PrintStream.println(String)
),
- 设置静态 class 成员。
所有这些切入点有什么共同点?没有目标对象,因为我们要么在谈论静态方法或成员、静态 class 初始化、对象预初始化(尚未定义 this
)或来自其他 [=] 的 calling/accessing 东西86=]没有包含我们在此处定位的注释。
所以您看到在 AspectJ 中两个切入点之间存在显着差异,在 Spring AOP 中它们只是由于其局限性而不引人注意。
如果您打算拦截目标对象实例中的非静态行为,我建议您使用 @target()
。如果您决定在 Spring 中激活 AspectJ 模式,或者甚至将一些代码移植到非 Spring、支持方面的应用程序,这将使切换到 AspectJ 变得更容易。
Spring 手册说:
any join point (method execution only in Spring AOP) where the target object has an @Transactional annotation: @target(org.springframework.transaction.annotation .Transactional)
any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation: @within(org.springframework.transaction.annotation .Transactional)
但我看不出它们有什么区别!
我试过 Google 它:
One difference between the two is that @within() is matched statically, requiring the corresponding annotation type to have only the CLASS retention. Whereas, @target() is matched at runtime, requiring the same to have the RUNTIME retention. Other than that, within the context of Spring, here is no difference between the join points selected by two.
所以我尝试添加带有 CLASS 保留的自定义注释,但是 Spring 抛出异常(因为注释 必须 运行时间保留)
您引用的信息是正确的,但是 只有 @target
个切入点指示符需要带有 RUNTIME
保留的注释,而 @within
只需要 CLASS
保留。
让我们考虑以下两个简单的注释:
ClassRetAnnotation.java
package mypackage;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.CLASS)
public @interface ClassRetAnnotation {}
RuntimeRetAnnotation.java
package mypackage;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RuntimeRetAnnotation {}
现在,如果你像下面这样定义一个方面,运行时不会有异常:
@Component
@Aspect
public class MyAspect {
@Before("@within(mypackage.ClassRetAnnotation)")
public void within() { System.out.println("within"); }
@Before("@target(mypackage.RuntimeRetAnnotation)")
public void target() { System.out.println("target"); }
}
我希望这个例子有助于阐明您指出的细微差别。
Spring参考:https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/core.html#aop-pointcuts
您没有注意到任何区别,因为 Spring AOP 在使用 AspectJ 语法时实际上只模拟了其功能的有限子集。因为SpringAOP是基于动态代理的,所以只提供public的拦截,非静态方法执行。 (当使用 CGLIB 代理时,您还可以拦截包范围和受保护的方法。)但是 AspectJ 还可以拦截方法调用(不仅仅是执行)、成员字段访问(静态和非静态)、构造函数 call/execution、静态 class 初始化等等。
那么让我们构建一个非常简单的 AspectJ 示例:
标记注释:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
驱动申请:
package de.scrum_master.app;
@MyAnnotation
public class Application {
private int nonStaticMember;
private static int staticMember;
public void doSomething() {
System.out.println("Doing something");
nonStaticMember = 11;
}
public void doSomethingElse() {
System.out.println("Doing something else");
staticMember = 22;
}
public static void main(String[] args) {
Application application = new Application();
application.doSomething();
application.doSomethingElse();
}
}
看点:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("@within(de.scrum_master.app.MyAnnotation) && execution(public !static * *(..))")
public void adviceAtWithin(JoinPoint thisJoinPoint) {
System.out.println("[@within] " + thisJoinPoint);
}
@Before("@target(de.scrum_master.app.MyAnnotation) && execution(public !static * *(..))")
public void adviceAtTarget(JoinPoint thisJoinPoint) {
System.out.println("[@target] " + thisJoinPoint);
}
}
请注意,我在这里通过向两个切入点添加 && execution(public !static * *(..))
来模拟 Spring AOP 行为。
控制台日志:
[@within] execution(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
Doing something
[@within] execution(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
Doing something else
这并不奇怪。这正是您在 Spring AOP 中也会看到的内容。现在,如果您从两个切入点中删除 && execution(public !static * *(..))
部分,在 Spring AOP 中,输出仍然相同,但在 AspectJ 中(例如,如果您在 Spring 中激活 AspectJ LTW),它会更改为:
[@within] staticinitialization(de.scrum_master.app.Application.<clinit>)
[@within] execution(void de.scrum_master.app.Application.main(String[]))
[@within] call(de.scrum_master.app.Application())
[@within] preinitialization(de.scrum_master.app.Application())
[@within] initialization(de.scrum_master.app.Application())
[@target] initialization(de.scrum_master.app.Application())
[@within] execution(de.scrum_master.app.Application())
[@target] execution(de.scrum_master.app.Application())
[@within] call(void de.scrum_master.app.Application.doSomething())
[@target] call(void de.scrum_master.app.Application.doSomething())
[@within] execution(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something
[@within] set(int de.scrum_master.app.Application.nonStaticMember)
[@target] set(int de.scrum_master.app.Application.nonStaticMember)
[@within] call(void de.scrum_master.app.Application.doSomethingElse())
[@target] call(void de.scrum_master.app.Application.doSomethingElse())
[@within] execution(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something else
[@within] set(int de.scrum_master.app.Application.staticMember)
当详细查看时,您会发现更多 @within()
个连接点被拦截,但也有更多 @target()
个连接点,例如call()
前面提到的连接点,还有 set()
用于非静态字段和对象 initialization()
在构造函数执行之前发生。
仅查看 @target()
时,我们会看到:
[@target] initialization(de.scrum_master.app.Application())
[@target] execution(de.scrum_master.app.Application())
[@target] call(void de.scrum_master.app.Application.doSomething())
[@target] execution(void de.scrum_master.app.Application.doSomething())
Doing something
[@target] set(int de.scrum_master.app.Application.nonStaticMember)
[@target] call(void de.scrum_master.app.Application.doSomethingElse())
[@target] execution(void de.scrum_master.app.Application.doSomethingElse())
Doing something else
对于这些方面输出行中的每一个,我们还会看到相应的 @within()
匹配项。现在让我们关注不一样的地方,过滤输出的差异:
[@within] staticinitialization(de.scrum_master.app.Application.<clinit>)
[@within] execution(void de.scrum_master.app.Application.main(String[]))
[@within] call(de.scrum_master.app.Application())
[@within] preinitialization(de.scrum_master.app.Application())
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something
[@within] get(PrintStream java.lang.System.out)
[@within] call(void java.io.PrintStream.println(String))
Doing something else
[@within] set(int de.scrum_master.app.Application.staticMember)
这里你看,按顺序排列
- 静态class初始化,
- 静态方法执行,
- 构造函数调用(尚未执行!),
- 构造对象预初始化,
- 另一个class(
System.out
)中的成员变量访问, - 从另一个调用方法 class (
PrintStream.println(String)
), - 设置静态 class 成员。
所有这些切入点有什么共同点?没有目标对象,因为我们要么在谈论静态方法或成员、静态 class 初始化、对象预初始化(尚未定义 this
)或来自其他 [=] 的 calling/accessing 东西86=]没有包含我们在此处定位的注释。
所以您看到在 AspectJ 中两个切入点之间存在显着差异,在 Spring AOP 中它们只是由于其局限性而不引人注意。
如果您打算拦截目标对象实例中的非静态行为,我建议您使用 @target()
。如果您决定在 Spring 中激活 AspectJ 模式,或者甚至将一些代码移植到非 Spring、支持方面的应用程序,这将使切换到 AspectJ 变得更容易。