我无法理解为什么在这种情况下应用建议@After
而不是@AfterThrowing
:
@Pointcut("execution(* componentB.Bye.run())")
public void newThread(){
}
@After("newThread()")
public void cokolwiek2(JoinPoint joinPoint){
report(joinPoint);
}
@AfterThrowing(pointcut="newThread()",throwing="e")
public void itsAFoo(JoinPoint joinPoint, RemoteException e) {
logger.error(joinPoint.getSignature().toLongString() + " exception here!");
}
我确信抛出异常:
public String greeting(String c) throws RemoteException,
InterruptedException {
throw new RemoteException();
//return "Good morning!";
}
但是这里没有异常的日志!
最佳答案
切入点execution(* componentB.Bye.run())
不覆盖方法public Stringgreeting(String c)
。
@After
和 @AfterThrowing
的区别在于,@AfterThrowing
仅在发生异常时调用,而 @After如果抛出异常或方法成功返回,则调用
。因此,如果出现异常并且您有两个建议,则两个建议都会被执行。
关于java - AspectJ中@After和@AfterThrowing的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848442/