java - 如何在AspectJ中使用@After和@AfterThrowing之一?

标签 java aspectj

在这种情况下如何避免双重记录?

@AfterThrowing(pointcut="execution(String greeting(..))",throwing="e")
public void itsAFoo1(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString() + " exception here!");
}

@After("execution(String greeting(..))")
public void itsAFoo2(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString());
}

我有两个日志:

[2012-08-07 17:02:01,585] [request1344351718430] [public java.lang.String componentC.Hello.greeting(java.lang.String) exception here!]
[2012-08-07 17:02:01,585] [request1344351718430] [public java.lang.String componentC.Hello.greeting(java.lang.String)]

但如果有异常(exception),我需要收到一份,如果没有异常(exception),我需要收到一份。

最佳答案

将@After更改为@AfterReturning

@AfterReturning("execution(String greeting(..))")
public void itsAFoo2(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString());
}

关于java - 如何在AspectJ中使用@After和@AfterThrowing之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11859568/

相关文章:

java - 如何对带注释的方法的每个方法调用执行某些操作?

java - 在 java 中使用正则表达式时转义 '{'?

java - AspectJ 使用 around 建议和 ProceedingJoinPoint 时遇到问题

java - spectj - 访问 thisJoinPoint.getTarget() 时出现堆栈溢出错误

java - 基于spring xml的bean定义, `bean`元素的类属性

java - AspectJ:LTW 中的 ExceptionInInitializerError

java - 将切面转换为原生 AspectJ 表示法

java - 24 位 wav 文件的数据 block 有符号字节还是无符号字节?

java - Java ftps连接,TrustManager说明(使用Filezilla服务器)

java - 显示MySQL数据库中的表名及其记录数