Java AspectJ“周围”和“继续”与“之前/之后”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18016503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AspectJ "around" and "proceed" with "before / after"
提问by Gérald Cro?s
Let's say you have three advices: around, beforeand after.
假设您有三个建议:around,before和after。
1) Are before/aftercalled when proceedis called in the aroundadvice, or are they called before/afterthe aroundadvice as a whole?
1)在around通知中调用proceed时是before/after调用,还是作为一个整体在around通知之前/之后调用?
2) If my aroundadvice does not call proceed, will the before/afteradvice be run anyway?
2) 如果我的around建议没有调用continue,之前/之后的建议会运行吗?
采纳答案by Frank M.
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);
}
}
i have following output
我有以下输出
- in around before execution(void aspects.TestAspect.hello())
- before execution(void aspects.TestAspect.hello())
- in hello
- after execution(void aspects.TestAspect.hello())
- in around after execution(void aspects.TestAspect.hello())
- in around before execution(void aspects.TestAspect.hello())
- in around after execution(void aspects.TestAspect.hello())
- 在执行之前(void aspect.TestAspect.hello())
- 执行前(void aspect.TestAspect.hello())
- 你好
- 执行后(void aspect.TestAspect.hello())
- 在执行后(void aspect.TestAspect.hello())
- 在执行之前(void aspect.TestAspect.hello())
- 在执行后(void aspect.TestAspect.hello())
so you can see before/after are not called when proceedis called from within @Around
annotation.
所以你可以看到当从注释中调用继续时,之前/之后没有被调用@Around
。
回答by user9759184
Que: 2) If my around advice does not call proceed, will the before/after advice be run anyway?
Que: 2) 如果我的 around 建议没有调用继续,之前/之后的建议会被运行吗?
Ans: If you don't call proceed in your around advice your before advice as well as your code execution will be skipped but your after advice will execute.But if your after advice use any value from that method everything will be null.So Practically there is no point of using that advice at all...
Ans:如果你没有在你的around通知中调用继续你的前通知和你的代码执行将被跳过但你的后通知将执行。但是如果你的后通知使用该方法中的任何值,一切都将是空的。所以实际上完全没有必要使用该建议......
Hope,I helped.
希望,我有所帮助。