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

时间:2012-08-08 07:19:39

标签: 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)]

但如果有异常,我需要收到一个,如果没有,我需要收到一个。

1 个答案:

答案 0 :(得分:2)

将@After更改为@AfterReturning

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