2013-08-27 69 views

回答

1

得到它由joinPoint.proceed刚刚登录的返回值工作()方法...

10

使用@AfterReturning和returnValue参数。

然后,您可以interogate返回的对象 这是我做的一切一个例子,但get方法在库

@AfterReturning(value = "@target(org.springframework.stereotype.Repository) && !execution(* get*(..))", returning = "returnValue") 
public void loggingRepositoryMethods(JoinPoint joinPoint, Object returnValue) { 
    String classMethod = this.getClassMethod(joinPoint); 



    if(returnValue !=null) 
    { 
     //test type of object get properties (could use reflection) 
     log it out 
    } 
    else 
    { 
     //do logging here probably passing in (joinPoint, classMethod); 
    } 
} 
+0

嗨Shaun,任何使用ProceedingJoinPoint类来完成此任务的方法? – prabu

+0

我发现它..我只是打印ProceedingJoinPoint的proceed()方法返回的对象。它打印返回的值。感谢您的回复.. :) – prabu

+0

伟大 - 感谢您的更新 –

3

在我们的情况下,大部分的时候,我们回位弹簧模式的实体类,所以我们覆盖所有实体类需要最小信息的toString方法和印刷如下

@AfterReturning(pointcut = "within(@org.springframework.stereotype.Service *)", returning = "result") 
public void logAfterReturning(JoinPoint joinPoint, Object result) { 
    logger.info(" ###### Returning for class : {} ; Method : {} ", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName()); 
    if (result != null) { 
     logger.info(" ###### with value : {}", result.toString()); 
    } else{ 
     logger.info(" ###### with null as return value."); 
    } 
}