侧边栏壁纸
博主头像
会飞的大象博主等级

爱运动的程序猿

  • 累计撰写 126 篇文章
  • 累计创建 158 个标签
  • 累计收到 0 条评论
标签搜索

目 录CONTENT

文章目录

使用AOP对接口做并发控制

会飞的大象
2021-10-26 / 0 评论 / 0 点赞 / 765 阅读 / 0 字

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SempAnnotation {
    String value();
}

接口:

@RestController
public class LimitController {
 
    @Autowired
    LimitService limitService;
 
    @SempAnnotation(value = "limitTest1")
    @RequestMapping(value = "/limitTest1",method = RequestMethod.GET)
    public String limitTest1(){
        return limitService.limitTest1();
    }
 
}

AOP:

package com.test.config;
 
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.Semaphore;
 
@Component
@Aspect
public class SemphoreAspect {
    private final Semaphore semphore=new Semaphore(10,true);
 
    @Pointcut(value = "@annotation(com.test.annotation.SempAnnotation)")
    public void semp(){
    }
    @Before("semp()")
    public void tryRequired(){
        try {
            System.out.println("请求");
            semphore.acquire();
        } catch (InterruptedException e) {
            System.out.println("限制中。。。。");
            e.printStackTrace();
        }
    }
    @After("semp()")
    public void after(){
        System.out.println("释放");
        semphore.release();
    }
}

0

评论区