有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

AspectJ中@After和@AfterThrowing之间的java差异

我无法理解为什么在这种情况下应用建议@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!";
}

但是没有带有exception here!的日志


共 (1) 个答案

  1. # 1 楼答案

    切入点execution(* componentB.Bye.run())不包括方法public String greeting(String c)

    @After@AfterThrowing之间的区别在于@AfterThrowing仅在发生异常时调用,而@After在抛出异常或方法成功返回时调用。因此,如果有一个例外,并且您有两个建议,那么它们都将被执行