springboot aop 拦截接口执行时间

时间:2023-03-09 18:11:16
springboot aop 拦截接口执行时间
/**
* @description: 记录接口执行时间日志的记录
* @author:
* @create 2018-12-27 16:32
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OptimizeLog {
}
package com.wsh.b2q.pc.aspectj;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; /**
* @description:
* @author: lvws
* @create 2018-12-27 16:34
*/
@Aspect
@Component
@Slf4j
public class OptimizeLogAspect {
@Pointcut("@annotation(OptimizeLog的路径)")
public void logPointCut() { } @Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
//开始时间
long start = System.currentTimeMillis();
//执行方法
Object result = point.proceed();
long end = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) point.getSignature(); //请求的方法名
String className = point.getTarget().getClass().getName();
String methodName = signature.getName();
log.debug("【接口执行时间】接口名:{}.{},执行时间:{}毫秒",className,methodName,(end-start));
log.info("【接口执行时间】接口名:{}.{},执行时间:{}毫秒",className,methodName,(end-start));
return result;
} }