假设您有三个建议:around、before 和 after。
1) 在 around 建议中调用 proceed 时是否会调用 before/after, 还是将它们称为 before/after around 建议作为一个整体?
2) 如果我的 around 建议没有调用 proceed, before/after 建议仍然会运行吗?
最佳答案
通过这个测试
@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。
关于java - AspectJ "around"和 "proceed"与 "before/after",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016503/