-
首先需要自定义注解@LogAnnotaion
//type代表可以放在类上面 method表示可以放在方法上 @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LogAnnotaion { String module() default ""; String operator() default ""; }
-
定义切面
@Component //能够让spring找到他 @Aspect //切面 定义了通知和切点的关系 @Slf4j public class LogAspect { @Pointcut("@annotation(com.itheima.common.aop.LogAnnotaion)")//切点 public void pt() { } //环绕通知 @Around("pt()") public Object log(ProceedingJoinPoint joinPoint) throws Throwable { long beginTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); recordLog(joinPoint, endTime - beginTime); return result; } private void recordLog(ProceedingJoinPoint joinPoint, long l) { System.out.println(); log.info("执行方法"+joinPoint.getTarget().getClass().getName() +"====执行时间:==="+ l); } }
-
在需要打印日志的方法上添加自定义注解
@LogAnnotaion(module = "检查项", operator = "添加检查项")
AOP 实现日志打印
最新推荐文章于 2024-01-27 17:27:30 发布