一.AOP
对于统计每一个业务方法的耗时这一操作,如果再业务层的每一个方法前获取方法运行的开始时间,方法结束获取结束时间,然后计算执行耗时,那这样就太繁琐了。能不能定义一个模板方法,使得该方法能够在业务层的方法执行时自动的获取开始时间和结束时间,从而计算方法的执行耗时。这就用到了AOP面向切面编程。

我们可以定义这么一个模板类,其中的的运行原始方法就是指的业务层中的特定方法。该技术类似于JavaSE中的动态代理技术,其实AOP最主流的实现方式就是动态代理。

二.Spring AOP快速入门
<!--AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
package com.gjw.aop;
/**
* spring-aop的快速入门程序
*/
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect // 指定当前为AOP类
public class TimeAspect {
@Around("execution(* com.gjw.service.*.*(..))") // 切入点表达式 第一个*代表返回值类型,*为任意 统计com.gjw.service包下的任意类(第二个*)下的任意方法(第三个*)的运行时间
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
// 1. 记录开始时间
long start = System.currentTimeMillis();
// 2. 调用原始方法运行
Object result = joinPoint.proceed(); // 通过形参joinPoint来调用要测试的方法
// 3. 记录结束时间
long end = System.currentTimeMillis();
log.info(joinPoint.getSignature() + "方法执行耗时:{}ms" ,end - start); // 通过joinPoint获取测试方法的签名
return result;
}
}
定义recordTime来封装这部分通用的逻辑。使用@Comonent注解将当前类交给IOC容器,使其成为IOC容器当中的bean对象。使用@Aspect注解指定当前为AOP类。使用@Around注解指定要统计哪些方法的执行耗时,在当前实例中,是要加在业务层的每一个方法上,第一个*代表方法返回值为任意。然后是包名,第二个*代表任意类/接口,第三个*代表任意方法。该表达式成为切入点表达式。
在运行原始方法是要借助AOP提供的API,该API为ProceedingJoinPoint,紧接着调用ProceedingJoinPoint的实例对象joinPoint的proceed()方法。该方法一运行就代表要运行原始的方法。原始方法在运行时可能会有异常,所以要往上抛。且原始方法在运行时可能有返回值,因此使用一个Object类型的变量来接受。再将result返回。
那么如何知道是哪个方法调用了proceed方法呢?在joinPoint对象中封装的就有原始方法的信息,调用其getSignature()方法就可以获得原始方法的签名。
三.统计耗时
当我们在前端开始项目时,点击部门管理,执行list()操作,统计到耗时为516ms。

点击编辑,执行getById()操作,统计到耗时为9ms。

四.应用场景和优势





















