An after-returning advice executes some extra code after the successful execution of the target method. In this recipe, we will log the return value of the target method.
Here are the steps for logging the return value of methods using an after-returning advice:
In your aspect class, create an advice method annotated with
@AfterReturning
. Make it take aJoinPoint
object and the return value of the target method as arguments:@AfterReturning(pointcut="execution(* com.spring_cookbook.controllers.*.*(..))", returning="returnValue") public void logReturnValue(JoinPoint joinPoint, Object returnValue) { ... }
In that advice method, log the return value preceded by the target method name:
String className = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); System.out.println...