AOP获取代理函数的参数名与值,参数不满足则不进入代理函数

本文介绍如何在AOP中获取代理函数的参数名和值,并实现参数不满足条件时阻止执行代理方法。示例代码展示了具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、
  1. 自定义一个方法:
private static Map<String, Object> getFieldsName(ProceedingJoinPoint point) throws ClassNotFoundException, NoSuchMethodException {
        Map<String,Object> map = new HashMap<>();
        //获取方法入参的数据
        Object[] paramObjs = point.getArgs();
        //1.这里获取到所有的参数值的数组
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //2.通过这获取到方法的所有参数名称的字符串数组
        String[] parameterNames = methodSignature.getParameterNames();
        //如果入参个数不为0
        for(int i=0;i<parameterNames.length;i++){
            map.put(parameterNames[i],paramObjs[i]);
        }
        return map;
    }
  1. 想要不进入代理方法,只能使用@Around和ProceedingJoinPoint。
    使用例子:
    /**
     * 判断文件是否过期
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around(value = "pointCut()")
    public Object paramValidateBefore(ProceedingJoinPoint point) throws Throwable {

        Map<String, Object> map = getFieldsName(point);

        if(null == map.get(BlacklistConst.UPLOAD_FILE_ID)){
            return Resp.build(ResponseCode.ERROR.getCode(), "文件已失效,请重新上传!", map);
        }
        String key = (String) map.get(BlacklistConst.UPLOAD_FILE_ID);
        if(ProgressManage.checkProgressMap.containsKey(key) || ProgressManage.importProgressMap.containsKey(key)){
            return point.proceed();
        }
        if(map.containsKey("response")){
            HttpServletResponse response = (HttpServletResponse) map.get("response");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode("导出异常.csv", "utf-8"));
            OutputStream stream = response.getOutputStream();
            stream.write("操作间隔大于15分钟,文件已失效,请重新上传!".getBytes("GBK"));
            return null;
        }
        return Resp.build(ResponseCode.ERROR.getCode(), "文件已失效,请重新上传!", map);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值