aop 获取参数并修改返回值

@Aspect
@Component
public class AsyncPageAop {

    /**
     * 定义切入点,用于匹配带有 @PreProcess 注解的方法。
     */
    @Pointcut("@annotation(com.arpa.wms.common.asyncPage.AsyncPage)")
    public void AsyncPage() {
    }

    /**
     * 在方法调用前进行处理,修改请求参数。
     * @param joinPoint 连接点对象
     * @return 修改后的返回结果
     * @throws Throwable 异常
     */
    @Around("AsyncPage() && @annotation(modifyReturnValueAnnotation)")
    public Object modifyParam(ProceedingJoinPoint joinPoint, AsyncPage modifyReturnValueAnnotation) throws Throwable {
        long startTime = System.currentTimeMillis();
        // 1.获取请求参数
//        Object[] args = joinPoint.getArgs();
//        Object arg = args[0];
//        // 2.查询selectType
//        Field selectTypeField = arg.getClass().getDeclaredField("selectType");
//        selectTypeField.setAccessible(true);
//        Object selectTypeValue = selectTypeField.get(arg);
       
        System.out.println("Select Type: " + selectType);
//        JSONObject jsonData = JSONObject.parseObject(JSON.toJSONString(arg));
//        System.out.println(jsonData.get("selectType"));
//        String selectType=String.valueOf(jsonData.get("selectType"));
        // 获取注解属性值
        String annotationValue = modifyReturnValueAnnotation.value();
        System.out.println("Annotation Value: " + annotationValue);
        // 根据入参修改返回值
        if (AsyncPageEnum.RECORDS.getValue().equals(selectType)&&AsyncPageEnum.RECORDS.getValue().equals(annotationValue)) {
            System.out.println("判断完成进入方法执行:"+ (System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE);
            Object result = joinPoint.proceed();
            return result;
        }else if (AsyncPageEnum.RECORDS.getValue().equals(selectType)&&AsyncPageEnum.COUNT.getValue().equals(annotationValue)) {
            Object modelInstance  =null;
            MethodSignature methodSignature  = (MethodSignature) joinPoint.getSignature();
            // 被切的方法
            Method method = methodSignature.getMethod();
            // 返回类型
            Class<?> methodReturnType = method.getReturnType();
            System.out.println(methodReturnType);
            // 实例化
           Map<String, Object> attributes = new HashMap<>();
           attributes.put("total", 0L);
           modelInstance = methodReturnType.getDeclaredConstructor().newInstance();
           setModelAttributes(modelInstance, attributes);
            System.out.println("判断完成返回count:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE);
            System.out.println(modelInstance);
            return modelInstance;
        }else if (AsyncPageEnum.COUNT.getValue().equals(selectType)&&AsyncPageEnum.COUNT.getValue().equals(annotationValue)) {
            System.out.println("判断完成进入方法执行:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE);
            Object result = joinPoint.proceed();
            return result;
        } else if (AsyncPageEnum.COUNT.getValue().equals(selectType)&&AsyncPageEnum.RECORDS.getValue().equals(annotationValue)) {
            System.out.println("判断完成返回list:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE);
            return new ArrayList<>();
        }
        // 调用目标方法,并获取返回值
        Object result = joinPoint.proceed();
        return result;
    }



    public void setModelAttributes(Object model, Map<String, Object> attributeMap) throws Exception {
        Class<?> modelClass = model.getClass();
        for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
            String propertyName = entry.getKey();
            Object propertyValue = entry.getValue();
            try {
                Field field = modelClass.getDeclaredField(propertyName);
                field.setAccessible(true);  // 设置为可访问
                field.set(model, propertyValue);  // 给属性赋值
            } catch (NoSuchFieldException e) {
                // 如果属性不存在,可以根据实际情况处理异常
                e.printStackTrace();
            }
        }
    }


}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AsyncPage {
    String value() default "";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值