自定义注解
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();
}
}
评论区