一.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;}}