Spring AOP思想与应用详解
一、AOP 简介
AOP(Aspect Oriented Programming,面向切面编程),可简单理解为面向特定方法编程。常见使用场景 :
-
统计业务方法执行耗时
-
日志记录
-
事务管理
-
权限控制
AOP 使用前后对比图
-
使用前
- 使用 AOP 后
AOP 优点 :
- 减少重复代码
- 代码无侵入
- 提高开发效率
- 维护方便
二、AOP 的使用方法
1. 引入 AOP 依赖
在 pom. xml
中添加如下依赖:
<dependency><groupId>org. Springframework. Boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. 编写 AOP 程序
以统计方法执行耗时为例:
@Component
@Aspect // 当前类为AOP类
@Slf4j
public class RecordTimeAspect {@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {long begin = System.currentTimeMillis();Object result = pjp.proceed();long end = System.currentTimeMillis();log.info("方法执行耗时: {}毫秒", end - begin);return result;}
}
三、AOP 核心概念
概念 | 说明 |
---|---|
连接点 (JoinPoint) | 可被 AOP 控制的方法,包含方法执行时的相关信息 |
通知 (Advice) | 抽取的共性功能 (如日志、耗时统计),最终体现为一个方法 |
切入点 (PointCut) | 匹配连接点的条件,决定通知应用于哪些方法 |
切面 (Aspect) | 通知+切入点,描述通知于切入点的对应关系,通常为带@Aspect 注解的类 |
目标对象 (Target) | 通知所应用的对象 |
AOP 底层通过动态代理机制实现对特定方法的增强。
四、Spring AOP 通知类型
注解 | 说明 |
---|---|
@Around | 环绕通知,此注解标注的通知方法在目标方法前、后都被执行 |
@Before | 前置通知,此注解标注的通知方法在目标方法前被执行 |
@After (重点) | 后置通知,此注解标注的通知方法在目标方法后被执行,无论是否有异常都会执行 |
@AfterReturning | 返回后通知,此注解标注的通知方法在目标方法后被执行,有异常不会执行 |
@AfterThrowing | 异常后通知,此注解标注的通知方法发生异常后执行 |
示例 |
@Slf4j
@Component
@Aspect
public class MyAspect1 {@Before("execution(* com.itheima.service.*.*(..))")public void before(JoinPoint joinPoint){log.info("before ...");}@Around("execution(* com.itheima.service.*.*(..))")public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {log.info("around before ...");Object result = proceedingJoinPoint.proceed();log.info("around after ...");return result;}@After("execution(* com.itheima.service.*.*(..))")public void after(JoinPoint joinPoint){log.info("after ...");}@AfterReturning("execution(* com.itheima.service.*.*(..))")public void afterReturning(JoinPoint joinPoint){log.info("afterReturning ...");}@AfterThrowing("execution(* com.itheima.service.*.*(..))")public void afterThrowing(JoinPoint joinPoint){log.info("afterThrowing ...");}
}
注意点:
- @Around 环绕通知需要手动调用 proceed () 方法执行原始方法,其他通知不需要考虑目标方法执行
- @Around 环绕方法的返回值,必须指定为 Object,来接收原始方法的返回值
五、@PointCut 抽取切入点方法
- @PointCut 的作用是将公共的切点表达式抽取出来,需要用到时引用该切点表达式即可, 提高代码复用性
示例:
@Pointcut("execution(* com.itheima.service.*.*(..))")
private void pt(){}@Before("pt()")
public void before(JoinPoint joinPoint){log.info("before ...");
}
- Private 修饰仅当前切面类可用, public 可被其他切面类引用。
六、@Order 通知顺序
- 多个切面类匹配同一目标方法时,默认按类名字母排序。
- 可用
@Order (数字)
注解指定顺序,数字越小优先级越高。 - 执行顺序
- 不同切面类中,默认按照切面类的类名字母排序
- 目标方法前的通知方法:字母排名靠前的先执行
- 目标方法后的通知方法:字母排名靠前的后执行
- 解决方法:用
@Order(数字)
加在切面类上控制顺序- 目标方法前的通知方法:数字小的先执行
- 目标方法后的通知方法:数字小的后执行
- 不同切面类中,默认按照切面类的类名字母排序
七、切入点表达式
1. execution 表达式
语法:? 代表当前可以省略,如访问修饰符(public,private)、包名,类名、异常
execution (访问修饰符? 返回值包名.类名.?方法名 (方法参数) throws 异常?)
- 常用通配符:
*
:匹配任意..
:匹配多层包或任意参数
@Before("execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
2. @annotation 表达式
- 用于匹配有特定注解的方法,简化切入点表达式书写
- 使用方法:
- 编写自定义注解
- 在业务类要作为连接点的方法上添加自定义注解
自定义注解示例:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogOperation{}
切面类示例
@Slf4j
@Component
@Aspect
public class MyAspect6 {@Before("@annotation(com.itheima.anno.LogOperation)")public void before(){log.info("MyAspect6 -> before ...");}@After("@annotation(com.itheima.anno.LogOperation)")public void after(){log.info("MyAspect6 -> after ...");}
}
拓展:切入点表达式的语法规则
可以使用通配符描述切入点
*
:单个独立的任意符号,可以通配任意返回值、包名、类名、方法名、任意类型的一个参数,也可以通配包、类、方法名的一部分..
:多个连续的任意符号,可以通配任意层级的包,或任意类型、任意个数的参数
切入点表达式的语法规则:
- 方法的访问修饰符可以省略
execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))
- 返回值可以使用
*
号代替(任意返回值类型)
execution(* com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))
- 包名可以使用
*
号代替,代表任意包(一层包使用一个*
)
execution(* com.itheima.*.*.DeptServiceImpl.delete(java.lang.Integer))
- 使用
..
配置包名,标识此包以及此包下的所有子包
execution(* com..DeptServiceImpl.delete(java.lang.Integer))
- 类名可以使用
*
号代替,标识任意类
execution(* com..*.delete(java.lang.Integer))
- 方法名可以使用
*
号代替,表示任意方法
execution(* com..*.*(java.lang.Integer))
- 可以使用
*
配置参数,一个任意类型的参数
execution(* com.itheima.service.impl.DeptServiceImpl.delete(*))
- 可以使用
..
配置参数,任意个任意类型的参数
execution(* com..*.*(..))
注意事项:
- 根据业务需要,可以使用且(&&)、或(||)、非(!) 来组合比较复杂的切入点表达式。
execution(* com.itheima.service.DeptService.list(..)) || execution(* com.itheima.service.DeptService.delete(..))
- 描述切入点方法通常基于接口描述,而不是直接描述实现类
建议: - 所有业务方法名在命名时尽量规范,方便切入点表达式快速匹配。如:查询类方法都是 find 开头,更新类方法都是update开头
- 在满足业务需要的前提下,尽量缩小点的匹配范围,例如包名匹配使用*匹配单个包
这篇文章到此就结束了,如果觉得有帮助,欢迎点赞、收藏、关注!