AOP快速入门
AOP快速入门
一、总述

二、代码
案例:记录每个方法的耗时
package com.itheima.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;//交给AOC容器管理
@Slf4j
@Component
//将这个类变为一个aoc类
@Aspect
public class TimeAspect {
// 指定作用的范围
// (* com.itheima.service.*.*(..))这是切入点表达式,代表com.itheima.service包下的类或接口中的所有方法@Around("execution(* com.itheima.service.*.*(..))")public Object recordTime(ProceedingJoinPoint joinedPoint) throws Throwable {// 记录开始时间long begin = System.currentTimeMillis();
// 调用原始方法运行// 调用AOP中提供的ProceedingJoinPoint的APIObject result = joinedPoint.proceed();
// 记录结束时间long end = System.currentTimeMillis();
// 输出日志
// joinedPoint.getSignature()是拿到原始方法的签名信息log.info(joinedPoint.getSignature()+"执行时间为:{}ms",end-begin);return result;}}