AspectJ“周围"和“继续"与“之前/之后" [英] AspectJ "around" and "proceed" with "before / after"

查看:31
本文介绍了AspectJ“周围"和“继续"与“之前/之后"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有三个建议:aroundbeforeafter.

Let's say you have three advices: around, before and after.

1) before/afteraround 通知中调用 proceed 时被调用,或者它们被称为之前/之后周围建议作为一个整体?

1) Are before/after called when proceed is called in the around advice, or are they called before/after the around advice as a whole?

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

2) If my around advice does not call proceed, will the before/after advice be run anyway?

推荐答案

With this Test

With this Test

@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 没有被调用.

so you can see before/after are not called when proceed is called from within @Around annotation.

这篇关于AspectJ“周围"和“继续"与“之前/之后"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆