JavaWeb:SpringBootAOP切面实现统计方法耗时和源码解析
介绍
快速入门
1.导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
`` `
2.切面类```java
@Slf4j
@Aspect
@Component
public class RecordTimeApsect {/*** 统计耗时* @param joinPoint* @return* @throws Throwable*/@Around("execution(* com.itheima.service.impl.EmpServiceImpl.*(..))")public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {// 1.获取当前时间long start = System.currentTimeMillis();// 2.执行目标方法Object result = joinPoint.proceed();// 3.得到运行时间long end = System.currentTimeMillis();log.info("运行时间:{}ms",(end-start));// 4.返回结果return result;}}
3.启动类EnableAspectJAutoProxy,加不加都行
核心概念
执行流程
通知类型
通知顺序(多个切面类)
切入点表达式-execution
切入点表达式-@annotation