我需要创建一个可用于记录返回值的注释

时间:2017-10-09 11:20:26

标签: spring spring-boot aspectj

对于用例,我们需要创建自定义注释,该注释将记录使用此注释注释的方法的返回值。 对于输入arg它很简单,但如何创建返回值?

1 个答案:

答案 0 :(得分:0)

使用@Around并记录结果:

@Aspect
public class YourAspect {
    @Around("@annotation(YourAnnotation) && execution(* *(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        Object returnObject = null;
        try {
            returnObject = joinPoint.proceed();
        } catch (Throwable throwable) {
            // You may want to log this
            throw throwable;
        }
        // log returnObject here...
        return returnObject;
    }
}