AspectJ"围绕"和"继续"与"之前/之后"

Gér*_*oës 28 java aop aspectj

比方说,你有三个建议:各地,之前和之后.

1)为前/后调用时继续被称为在各地的意见,或者他们所谓的前/后的周围建议作为一个整体?

2)如果我的周围建议没有调用继续,那么是否会运行之前/之后的建议?

Fra*_* M. 38

有了这个测试

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下输出

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

所以你可以看到之前/之后没有被调用

  • 所以你可以看到,当从@Around注释中调用proceed时,before/after不会被调用。这不是完全相反吗? (2认同)