java - AspectJ中@After和@AfterThrowing的区别

标签 java log4j aspectj

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

相关文章:

java - 运行时发生log4j.PropertyConfigurator错误说找不到适合configure(FileInputStream)的方法

java - Log4j 记录器垃圾收集?

java - log4j.xml 被忽略

java - 双代理接口(interface) : Could not generate CGLIB subclass of class 时 Spring AspectJ 失败

java - Docker 错误 : Could not find or load Main class Main. 主要

java - 在线程中运行函数或在函数中使用线程

java - spring security ldap隐藏密码属性

Java AOP在构造对象时设置字段值

java - 如何为无法更新任何实例变量的方法编写方面或注释?

java - 我如何阅读和打印 Lucene 索引 4.0