八、AOP
8.1 AOP基础
AOP:面向切面编程,就是面向特定方法编程。
场景:统计每一个业务方法的执行耗时。
传统解决办法:获取方法运行开始时间跟方法运行结束时间,然后做差得到执行耗时。缺点是繁琐(项目中的方法多了去了)
AOP:
实现:动态代理是面向切面编程最主流的实现。而SpringAOP是Spring框架的高级技术,旨在管理bean对象的过程中,主要通过底层的动态代理机制,对特定的方法进行编程。
快速入门
-
导入依赖:在pom.xml中导入AOP依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
-
编写AOP程序:针对于特定方法根据业务需要进行编程,而不需要关注其他非该接口关注的逻辑或处理
@Slf4j @Component @Aspect public class TimeAspect { @Around("execution(* com.itheima.service.*.*(..))")//切入点表达式。所有类、所有接口中的方法 public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable { //1.记录开始时间 long begin = System.currentTimeMillis(); //2.调用原始方法 Object result = joinPoint.proceed(); //3.记录结束时间,计算耗时方法 long end = System.currentTimeMillis(); log.info(joinPoint.getSignature()+"方法执行耗时:{}ms",end-begin); return result; } }
此后每次执行业务层的方法时都会进行计算业务耗时。
AOP核心概念
-
连接点:JoinPoint,可以被AOP控制的方法(暗含方法执行时的相关信息。在上个例子中的com.itheima.service包中所有方法都是连接点)
-
通知:Advice,指哪里重复的逻辑,也就是共性功能(最终体现为一个方法)
//上个例子为例,这个方法就是通知 public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable { //1.记录开始时间 long begin = System.currentTimeMillis(); //2.调用原始方法 Object result = joinPoint.proceed(); //3.记录结束时间,计算耗时方法 long end = System.currentTimeMillis(); log.info(joinPoint.getSignature()+"方法执行耗时:{}ms",end-begin); return result; }
-
切入点:PointCut,匹配连接点的条件,通知仅会在切入点方法执行时被应用(实际被AOP控制的方法),通常以切入点表达式来描述。
@Around("execution(* com.itheima.service.*.*(..))")//切入点表达式。
-
切面:Aspect,描述通知与切入点的对应关系(通知+切入点),而注解@Aspect称作切面类
-
目标对象:Target,通知所应用的对象。
进行AOP开发,最终运行的不是目标对象,而是基于目标生成的代理对象proxy
8.2 AOP进阶
通知类型
通知类型 | 作用 |
---|---|
@Around | 环绕通知。在连接点前后执行 |
@Before | 前置通知。在连接点前被执行 |
@After | 后置通知。在连接点后执行,无论是否有异常 |
@AfterReturning | 返回后通知。在连接点正常执行完成后执行,如果连接点抛出异常就不会执行 |
@AfterThrowing | 异常后通知。连接点发生异常后执行 |
注意事项:
@Around环绕通知需要自己调用 ProceedingJoinPoint.proceed() 来让原始方法执行,其他通知不需要考虑目标方法执行
@Around环绕方法的返回值,必须指定为Object,来接收原始方法的返回值。
@PointCut
该注解的作用是将公共的切点表达式抽取出来,需要用到时引用该切点表达式即可。
//使用@PointCut之前,在多个方法前面都要标注切入点跟切入点表达式
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public Object a(ProceedingJoinPoint joinPoint){
}
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public Object b(ProceedingJoinPoint joinPoint){
}
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public Object c(ProceedingJoinPoint joinPoint){
}
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public Object d(ProceedingJoinPoint joinPoint){
}
//使用PointCut之后
@PointCut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
public void xx(){}
/*
public:仅能在当前切面类中使用该切入点表达式
private:在其他的外部切面类中也可使用该表达式
*/
@Around("xx()")
public Object a(ProceedingJoinPoint joinPoint){
}
@Around("xx()")
public Object b(ProceedingJoinPoint joinPoint){
}
@Around("xx()")
public Object c(ProceedingJoinPoint joinPoint){
}
@Around("xx()")
public Object d(ProceedingJoinPoint joinPoint){
}
通知顺序
默认情况下,不同切面类通知的执行顺序按照切面类的类名字母排序。
- 目标方法前的通知方法,字母排名靠前的先执行
- 目标方法后的通知方法,字母排名靠前的后执行
用@Order(数字)加在切面类上来控制顺序
- 目标方法前的通知方法,数字小的先执行
- 目标方法后的通知方法,数字大的先执行
切入点表达式
execution
匹配多个方法
根据业务需要,可以使用且或非来组合比较复杂的切入点表达式
@annotation
先创建一个注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog{
}
然后在需要调用的方法上添加创建的注解
@MyLog
@Override
public List<Dept> list(){
List<Dept> deptList = deptMapper.list();
return deptList;
}
然后用注解@annotation来匹配带有已创建注解的方法
@PointCut("annotation(com.itheima.aop.MyLog)")
private void pt(){}
启动之后,就会执行根据注解匹配到的方法。
连接点
在Spring中用JoinPoint抽象了连接点,用它可以获得方法执行时的相关信息,如目标类目、方法名、方法参数等
-
对于@Around通知,获取连接点信息只能使用 ProceedingJoinPoint
@Around("execution(xxx)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ String className = joinPoint.getTarget().getClass().getName(); //获取目标类名 Signature signature = joinPoint.getSignature(); //获取目标方法签名 String methodName = joinPoint.getSignature().getName(); //获取目标方法名 Object[] args = joinPoint.getArgs(); //获取目标方法运行参数 Object res = joinPoint.proceed(); //执行原始方法,获取返回值(环绕通知) return res; }
-
对于其他四种通知,获取连接点信息只能使用 JoinPoint ,它是 ProceedingJoinPoint 的父类型
@Before("execution(xxx)") public void before (JoinPoint joinPoint){ String className = joinPoint.getTarget().getClass().getName(); //获取目标类名 Signature signature = joinPoint.getSignature(); //获取目标方法签名 String methodName = joinPoint.getSignature().getName(); //获取目标方法名 Object[] args = joinPoint.getArgs(); //获取目标方法运行参数 }