gpt4 book ai didi

java - @target 和 @within 之间的区别(Spring AOP)

转载 作者:行者123 更新时间:2023-12-02 20:34:55 29 4
gpt4 key购买 nike

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)



但我看不出它们之间有什么区别!

我试图谷歌它:

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 抛出异常(因为注释 必须具有 RUNTIME 保留)

最佳答案

您没有注意到任何区别,因为 Spring AOP 在使用 AspectJ 语法时,实际上只模拟了其功能的有限子集。因为 Spring AOP 是基于动态代理的,它只提供对公共(public)的、非静态方法执行的拦截。 (当使用 CGLIB 代理时,您还可以拦截包范围和 protected 方法。)然而,AspectJ 也可以拦截方法调用(不仅仅是执行)、成员字段访问(静态和非静态)、构造函数调用/执行、静态类初始化等等。

因此,让我们构建一个非常简单的 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)

在这里你看,在外观上
  • 静态类初始化,
  • 静态方法执行,
  • 构造函数调用(尚未执行!),
  • 构造对象预初始化,
  • 另一个类(System.out)中的成员变量访问,
  • 从另一个类(PrintStream.println(String))调用方法,
  • 设置静态类成员。

  • 所有这些切入点有什么共同点?没有目标对象,因为我们谈论的是静态方法或成员、静态类初始化、对象预初始化(尚未定义 this)或从其他不带有我们在此处定位的注释的类中调用/访问内容。

    所以你看到,在 AspectJ 中,两个切入点之间存在显着差异,在 Spring AOP 中,由于其局限性,它们并不明显。

    我对你的建议是使用 @target()如果您的意图是拦截目标对象实例中的非静态行为。如果您决定在 Spring 中激活 AspectJ 模式,甚至将一些代码移植到非 Spring、启用方面的应用程序,这将使切换到 AspectJ 变得更加容易。

    关于java - @target 和 @within 之间的区别(Spring AOP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51124771/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com