gpt4 book ai didi

java - AspectJ "around"和 "proceed"与 "before/after"

转载 作者:IT老高 更新时间:2023-10-28 21:15:09 28 4
gpt4 key购买 nike

假设您有三个建议:aroundbeforeafter

1) 在 around 建议中调用 proceed 时是否会调用 before/after,还是将它们称为 before/after around 建议作为一个整体?

2) 如果我的 around 建议没有调用 proceedbefore/after 建议仍然会运行吗?

最佳答案

通过这个测试

@Aspect
public class TestAspect {
private static boolean runAround = true;

public static void main(String[] args) {
new TestAspect().hello();
runAround = false;
new TestAspect().hello();
}

public void hello() {
System.err.println("in hello");
}

@After("execution(void aspects.TestAspect.hello())")
public void afterHello(JoinPoint joinPoint) {
System.err.println("after " + joinPoint);
}

@Around("execution(void aspects.TestAspect.hello())")
public void aroundHello(ProceedingJoinPoint joinPoint) throws Throwable {
System.err.println("in around before " + joinPoint);
if (runAround) {
joinPoint.proceed();
}
System.err.println("in around after " + joinPoint);
}

@Before("execution(void aspects.TestAspect.hello())")
public void beforeHello(JoinPoint joinPoint) {
System.err.println("before " + joinPoint);
}
}

我有以下输出

  1. 在执行前(void aspect.TestAspect.hello())
  2. 执行前(void aspect.TestAspect.hello())
  3. 你好
  4. 执行后(void aspect.TestAspect.hello())
  5. 在执行后(void aspect.TestAspect.hello())
  6. 在执行之前(void aspect.TestAspect.hello())
  7. 在执行后(void aspect.TestAspect.hello())

所以你可以看到在 @Around 注释中调用 proceed 时没有调用 before/after。

关于java - AspectJ "around"和 "proceed"与 "before/after",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016503/

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