1,定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface PrintLog {
}
2,定义注解实现
@Aspect
@Component
@Slf4j
public class LogDebug {
@Pointcut("@annotation(com.biz.primus.ms.base.log.PrintLog)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
Object result = point.proceed();
long time = System.currentTimeMillis() - beginTime;
MethodSignature signature = (MethodSignature)point.getSignature();
String className = signature.getMethod().getName();
log.debug("执行方法:{},耗时:{}ms,入参:{}",className,time,new Gson().toJson(point.getArgs()));
return result;
}
}
注意配置包扫描和开启代理@EnableAspectJAutoProxy(proxyTargetClass = true)