0
点赞
收藏
分享

微信扫一扫

如何自定义注解对返回值操作

快乐与微笑的淘气 2022-03-10 阅读 56
javaspring

如何自定义注解对返回值操作

第一步

@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface LogAnnotation {

    /**
     * 模块名字
     * @return
     */
    String modelName() default "";

    /**
     * 模块类型
     * @return
     */
    String option();
}


第二步


@Aspect
@Component
public class LogAspect {

// 两种写法都可
    @Pointcut("@annotation(cn.stylefeng.guns.modular.test.annotation.LogAnnotation)")
//    @Pointcut("execution( * cn.stylefeng.guns.modular.InternetBehavir.controller.*.*(..))")
    public void operLogPoinCut(){
    }

//    标注此方法体为后置通知,当目标方法执行成功之后执行该方法
//    @AfterReturning(value = "operLogPoinCut()",returning = "result")
    @Around("operLogPoinCut()")
    public Object addLogSuccess(ProceedingJoinPoint joinPoint) throws Throwable{

        Object proceed = joinPoint.proceed();

        System.out.println(111111111);

        if (!(proceed instanceof ResponseData)){
            System.err.println("返回的结果不是ResponseData类型,直接返回原值");
            return proceed;
        }

        ResponseData responseData = (ResponseData) proceed;
        Object data = responseData.getData();

        Map collection = (Map) data;

        collection.put("这是新的","这是新的测试");
        
        return responseData;

    }

}

运行结果

在这里插入图片描述

举报

相关推荐

0 条评论