Thursday, March 12, 2009

Logging Return Value and Method Arguments with Spring AOP

as in the previous example, but use this:


<aop:after-returning method="logExitAfterReturn" ref="loggingPointcut" returning="retVal"></aop:after-returning>


and the method becomes:

public void logExitAfterReturn(final JoinPoint joinPoint, Object retVal)

{
log( "Exiting method (after return) " + joinPoint.getSignature().getName() + ", retValue = " + retVal + ".");
}


while the logEntry becomes:

public void logEntry(final JoinPoint joinPoint)

{

StringBuilder arguments = new StringBuilder();

arguments.append(" args= ");

for (Object arg : joinPoint.getArgs()) {

arguments.append(arg).append(", ");

}

log("Entering method " + joinPoint.getSignature().getName() + arguments.toString() + "...");

}




No comments: