Search code examples
javacastingreturn-valuespring-aoppointcut

AOP pointcut for @After


I want AOP logging for the method's return value

    @After("execution(* com.dbs.tup.sample.service.*.*(..))")
    public void logAfter(JoinPoint point) {
        log.info("{} is returning {}", point.getSignature().getName(), "???");
    }

Solution

  • I found this

        @AfterReturning(pointcut = "execution(* com.dbs.tup.sample.service.*.*(..))", returning = "retValue")
        public void logAfter(JoinPoint point, Object retValue) {
            log.info("{} is returning {}", point.getSignature().getName(), (((MethodSignature) point.getSignature()).getReturnType().cast(retValue)).toString());
        }