AOP拦截接口,获取访问次数

 首先、需要一个自定义注解,如下:

1、

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {

}

 2、

@Aspect
@Component
public class AuthorizationAspect {
    @Resource
    private CountVOMapper countVOMapper;

    /**
     * 定义切点
     */
    @Pointcut("@annotation(com.bc.mcode.common.Authorization)")
    public void executeService() {
    }

    /**
     * 环绕织入
     */
    @Around("executeService()&&@annotation(authorization)")
    public Object proceed(ProceedingJoinPoint thisJoinPoint, Authorization authorization) {
        Object proceed = null;
        //获取到请求参数
        Map<String, Object> fieldsName = getFieldsName(thisJoinPoint);
       
        //这里是做业务逻辑的,如果是做访问次数的,需要有个表存次数,在这里获取并且做  i++,在更新数据库

        try {
            proceed = thisJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return proceed;
    }
    /**
     * 获取接口上的参数列表,封装成map
     * @param joinPoint
     */
    private static Map<String, Object> getFieldsName(ProceedingJoinPoint joinPoint) {
        // 参数值
        Object[] args = joinPoint.getArgs();
        ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = pnd.getParameterNames(method);
        Map<String, Object> paramMap = new HashMap<>(32);
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
        }
        return paramMap;
    }
}

3、最后一步是吧注解放在你需要监控的接口:

 
    @RequestMapping("hhhh")
    @ResponseBody
    @Authorization
    public GaxxVO queryGaxxVOById(Integer id,String types){
       
        return gaxxVOService.queryByPrimaryKey(id);
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值