31

3 つのアドバイスがあるとしましょう: aroundbeforeafter

1)前後のアドバイスで手続きが呼び出されたときに呼び出されますか、それとも前後のアドバイス全体で呼び出されますか?

2)私の周りのアドバイスがproceedを呼び出さない場合、前/後のアドバイスはとにかく実行されますか?

4

2 に答える 2

43

このテストで

@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. 実行前あたり(voidspects.TestAspect.hello())
  2. 実行前(voidspects.TestAspect.hello())
  3. こんにちは
  4. 実行後(無効アスペクト.TestAspect.hello())
  5. 実行後あたり(voidspects.TestAspect.hello())
  6. 実行前あたり(voidspects.TestAspect.hello())
  7. 実行後あたり(voidspects.TestAspect.hello())

したがって、アノテーション内からprocedureが呼び出されたときにbefore/afterが呼び出されないことがわかります@Around

于 2013-08-05T09:21:24.927 に答える