基于ssm项目自定义AOP注解实现限流步骤

本文介绍如何在SSM项目中通过自定义AOP注解实现接口限流,包括定义注解类、切面类及XML配置,确保服务稳定运行。

基于ssm项目自定义AOP注解实现限流步骤

具体操作步骤如下

第一步:定义自定义注解类

添加注解类 new Annotation
具体实现如下

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface InterfaceCustom {
    //可定义参数
}

第二步:定义切面类

添加切面类new Class
具体实现如下

@Pointcut("@annotation(com.thunisoft.zbjs.api.metadata.InterfaceCustom)")
    public void controllerAspect() {
        System.out.println("我是一个切入点");
    }
@Around("controllerAspect()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = null;
        try {
            if(不满足限流条件){
                //执行访问方法
                result = joinPoint.proceed();
            }else {
                Map<String,Object> map = new HashMap<>();
                result = map.put("error","请求太过频繁,请稍后重试!");
            }
        } catch (Throwable e) {
            // 异常通知
            result = Response.error("请求失败,服务器内部错误!");
            throw new RuntimeException(e);
        }
        // 后置通知

        return result;

    }

第三步:在xml中配置aop

xml头的位置bean中添加aop配置
具体实现如下

xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation最后面添加
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop.xsd

//下面添加
<aop:aspectj-autoproxy proxy-target-class="true"/>

第四步:在需要的方法上添加注解(本人以controller中方法为例,其他雷同)

xml头的位置bean中添加aop配置
具体实现如下

 @InterfaceCustom
 @RequestMapping("/")
 public Object getTest() {     
     Object o = null;
     return o;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值