当前位置: 首页 > news >正文

Spring AOP 使用方法总结

AOP切面编程的最佳应用场景

  • 记录日志
  • 性能监控
  • 事务管理
  • 处理异常
  • 数据验证,验证传入参数的正确性(一般不用这个方法做,而是用拦截器)

spring提供了以下注解供开发者使用,编写AOP程序

  • @Aspect 申明切面
  • @Pointcut 切点,申明AOP的作用范围(如:一个类下所有方法,某个方法,带有指定注解的方法)
  • @Before 前置通知
  • @After 后置通知
  • @Around 环绕通知
  • @AfterThrowing 异常通知,当被切入点中代码执行异常时触发

实操

举个例子

  1. 定义
    @RestController
    @RequestMapping(path = {"test"})
    public class DemoController {@GetMapping(path = {"test01/{name}"})public ResponseEntity<String> test01(@PathVariable("name") String name) {return new ResponseEntity<>(name.toUpperCase(), HttpStatus.OK);}
    }
    
    如果我希望在上面方法使用AOP,定义一个类,实现如下
    @Slf4j
    @Aspect
    @Component
    public class AOPAction {// 定义切点,标记此切点在什么范围内此切面起作用@Pointcut("execution(* com.train.controller.DemoController.test02(..))")public void pointcut() {}//前置通知@Before(value = "pointcut()")public void before(JoinPoint joinPoint) {log.info("Before Method : {}...{}", joinPoint.getSignature().getName(), args[0].toString());}// 后置通知@After(value = "pointcut()")public void after(JoinPoint joinPoint) {log.info("After Method : {}...{}", joinPoint.getSignature().getName(), args[0].toString());}// 环绕通知@Around(value = "pointcut()")public Object around1(ProceedingJoinPoint joinPoint) throws Throwable {log.info("Around 1 Method before: {}...", joinPoint.getSignature().getName());Object result = joinPoint.proceed();log.info("Around 1 Method after: {}...", joinPoint.getSignature().getName());return result;}
    }
    

切点执行范围

此章节介绍多种切点定义形式,开发中使用哪一种视情况而定

1.指定包下所有类的所有方法

应用在此包下所有类的所有方法上

// 包名:com.train.controller
@Pointcut("execution(* com.train.controller..*.*(..))")
public void pointcut() {}

2.指定类中所有方法

应用在指定类的所有方法上

// 全类名:com.train.controller.DemoController
@Pointcut("execution(* com.train.controller.DemoController.*(..))")
public void pointcut() {}

3.指定某个类中某个方法

应用在指定类的指定方法上,而此类中的其他方法不会被应用

// 全类名:com.train.controller.DemoController
@Pointcut("execution(* com.train.controller.DemoController.test01(..))")
public void pointcut() {}

4.1 按参数数量指定

AOP切入点表达式规范中,用*表示一个参数,用表示无固定个数参数

可以这么理解:把*理解成通配符,且仅匹配一个。

// 仅作用在com.train.controller包下所有类中仅有2个参数的方法上
@Pointcut(value = "execution(* com.train.controller..*.*(*,*))")
public void pointcut02(){}
// 仅作用在com.train.controller包下所有类中仅有5个参数的方法上
@Pointcut(value = "execution(* com.train.controller..*.*(*,*,*,*,*))")
public void pointcut02(){}

4.2 按参数注解指定

⚠️ 研究中,未完成


5.加有指定注解的方法

仅应用在使用了注解的方法上,可以使用自定义注解,也可以使用框架提供注解。此形式最灵活,推荐开发中使用

// 自定义注解
public @interface LogPointCut {}
// 加注解的方法
@GetMapping(path = {"test/{name}"})
@LogPointCut
public ResponseEntity<String> test(@PathVariable("name") String name) {return new ResponseEntity<>(name.toUpperCase(), HttpStatus.OK);
}
// 定义
@Pointcut("@annotation(com.train.annotation.LogPointCut)")
public void pointcut() {
}

6.组合条件【难点】

使用A符合 Spring AOP 的切入点表达式规范组合多个条件,组合条件最好用&&连接各个条件。用+匹配接口。用*匹配一个。用匹配无固定数

举例

// 仅作用在com.train.controller包下所有类中所有标记了@LogPointCut注解的方法上
@Pointcut(value = "execution(* com.train.controller..*.*(..)) && @annotation(com.train.spr.annotation.LogPointCut)")
public void pointcut02(){}
// 仅作用在com.train.spr.controller包下所有实现了MyInterface接口的类的方法上
@Pointcut(value = "execution(* (com.train.controller..* && com.train.spr.interfaces.MyInterface+).*(..))")
public void pointcut02(){}// 仅作用在com.train.spr.controller包下所有实现了MyInterface接口和MyInterface2接口的类的方法上
@Pointcut(value = "execution(* (com.train.controller..* " +"&& com.train.spr.interfaces.MyInterface+ " +"&& com.train.spr.interfaces.MyInterface2+ ).*(..))")
public void pointcut02(){}

当然!也可以组合多个已定义的条件作为一个条件,例如

import org.aspectj.lang.annotation.Pointcut;// 仅作用在com.train.spr.controller包下类的方法上
@Pointcut(value = "execution(* com.train.controller..*.*(..))")
public void pointcut01() {}// 仅作用在实现了MyInterface接口的类的方法上
@Pointcut(value = "execution(* com.train.spr.interfaces.MyInterface+.*(..))")
public void pointcut02() {}// 组合上述两个切入点的条件
@Pointcut(value = "pointcut02() && pointcut01()")
public void pointcut03() {}

AOP切入点表达式规范请查阅官网

获取参数

AOP的方法可以获取被切入方法的参数,但是修改这些参数并不会在被切入方法中生效。举个例子,方法A有一个前置通知方法B,且方法B获取方法A的参数,即便方法B中修改了参数值,方法A获取到的参数仍然不会改变,原来是怎样就是怎样

获取参数这块注意3点就够了。

  1. 前置通知/后置通知的参数是org.aspectj.lang.JoinPoint
  2. 环绕通知的参数是org.aspectj.lang.ProceedingJoinPoint
  3. 被切入点的参数信息都被封装到JoinPointProceedingJoinPoint中,直接从中获取即可【注意类型】

特殊的切入方法——异常通知

研究中,未完成

http://www.lryc.cn/news/471692.html

相关文章:

  • LeetCode 每日一题 2024/10/21-2024/10/27
  • 不到1500元的I卡可以玩转3A大作吗?撼与科技Intel Arc A750显卡游戏性能实
  • STK与MATLAB互联——仿真导航卫星与地面用户间距离和仰角参数
  • js面试问题笔记(一)
  • pip 和 pipx 的主要区别?
  • 4457M数字示波器
  • 【永中软件-注册/登录安全分析报告】
  • Tomcat作为web的优缺点
  • conda虚拟环境中安装cuda方法、遇到的问题
  • 【CPN TOOLS建模学习】设置变迁的属性
  • 一个简单的例子,说明Matrix类的妙用
  • 【C++】类和对象(四):析构函数
  • linux中各目录作用及介绍
  • v4.7版本使用线下付款方式不给管理员发送新订单通知问题修复
  • vue3中mitt和pinia的区别和主要用途,是否有可重合的部分?
  • 飞书文档解除复制限制
  • vue3中ref和reactive的用法,区别和优缺点,以及使用场景
  • 电脑技巧:Rufus——最佳USB启动盘制作工具指南
  • vue的基本使用
  • C#高级:利用 CancellationToken 实现方法超时控制,提升应用响应性
  • Java Lock LockSupport 总结
  • 线性表之链式存储基本操作(c语言实现,附解析)
  • 27.Redis哨兵架构
  • BGP路由优选
  • cjson内存泄漏问题注意事项
  • 雷军救WPS“三次”,WPS注入新生力量,不再“抄袭”微软
  • zookeeper全系列学习之分布式锁实现
  • 耐用的内衣洗衣机有哪些?双11好用内衣洗衣机品牌排行榜
  • 富格林:曝光可信经验击败陷阱
  • 3211、生成不含相邻零的二进制字符串-cangjie