使用切面,实现打印方法描述问题(springboot+mybatis)

1.新建工具类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodDescription {
    String value();
}
 
 

在mapper接口上写上@MethodDescription("描述")

2.

@Aspect
@Component
public class HandlerTimeAspect {
   @Pointcut("execution(* 地址.*.*(..))")
   public void pointcut() {}
   @Around("pointcut()")
   public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable {
      String methodName = pjd.getSignature().getName();
      long begin = System.currentTimeMillis();
      MethodSignature methodSignature = (MethodSignature)pjd.getSignature();
      Method targetMethod = methodSignature.getMethod();
      MethodDescription methodDescription = targetMethod.getAnnotation(MethodDescription.class);
      String methodDesc ="";
      if(!NullCheckUtils.checkObjectIsNull(methodDescription)){
         methodDesc = methodDescription.value();
      }
      Logger.info("开始执行SQL{}({}[{}])",methodDesc,methodName,begin);
      Object result = null;
      try {
         result = pjd.proceed();
         methodName = pjd.getSignature().getName();
      } catch (Throwable e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         throw e;
      }
      long end = System.currentTimeMillis();
      long useTime = end - begin;
      Logger.info("执行结束SQL{}({}[{}]),耗时{}ms",methodDesc,methodName,begin,useTime);
      return result;
   }
}

猜你喜欢

转载自blog.csdn.net/qq_35193093/article/details/80313821