本文介绍了AspectJ“周围"和“继续"与“之前/之后"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有三个建议:around、before 和 after.
Let's say you have three advices: around, before and after.
1) before/after 在 around 通知中调用 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 建议没有调用 proceed,before/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);
}
}
我有以下输出
- 在执行之前(void aspect.TestAspect.hello())
- 执行前(void aspect.TestAspect.hello())
- 你好
- 执行后(void aspect.TestAspect.hello())
- 在执行后(void aspect.TestAspect.hello())
- 在执行之前(void aspect.TestAspect.hello())
- 在执行后(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“周围"和“继续"与“之前/之后"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!