🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 CSDN🚩🚩🚩
🎈由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!🌠个人主页
🌟 一、前言
接口幂等性使用AOP处理与使用拦截器处理的步骤大致相同,本篇旨在阐述两者的区别,具体的实现方式见博文Redis处理 Spring Boot 接口幂等性【使用拦截器处理版本】
🌟 二、AOP处理与拦截器处理区别
- AOP处理可以处理几乎所有的注解包括Controller和Servive中的
- 拦截器处理只能处理Controller中加入的注解,而Service中的注解就不能够使用它
🌟 三、AOP处理实现
只需要删除掉拦截器处理中的拦截器部分,其他的代码一样,额外加入下方代码
@Component
@Aspect
public class IdempotentAspect {
@Autowired
tokenService tokenService;
//拦截处理带@AutoIdempoten注解的方法
@Pointcut("@annotation(com.dong.interfacemiden.anno.AutoIdempotent)")
public void pc1(){
}
@Before("pc1()")
public void before() throws IdempotentException {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
try {
tokenService.checkToken(request);
} catch (IdempotentException e) {
throw e;
}
}
}