AspectJ“围绕"和“进行"与“之前/之后"一起使用 [英] AspectJ "around" and "proceed" with "before / after"

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

问题描述

假设您有3条建议:周围之前之后.

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

1)在周围建议中调用进行时,在之前/之后被调用, 还是整体上将它们称为前后建议之前?

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)如果我的周围建议未致电进行之前/之后建议是否会继续运行?

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

推荐答案

通过此测试

@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. 在执行之前(无效方面.TestAspect.hello())
  2. 在执行之前(没有方面.TestAspect.hello())
  3. 打招呼
  4. 执行后(无效方面.TestAspect.hello())
  5. 执行后的周围(无效方面.TestAspect.hello())
  6. 在执行之前(无效方面.TestAspect.hello())
  7. 执行后的周围(无效方面.TestAspect.hello())

因此您可以看到从@Around批注中调用进行时未调用之前/之后.

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

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

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