• 自定义注解+切面,环绕通知打印方法日志


    自定义注解+切面,环绕通知打印方法日志

    一、自定义注解类
    public @interface XXXInterface {
    }

    二、切面类
    @Component
    @Aspect
    public class XXXInterfaceAspect {

    private static final Logger US_LOG = LogBuilderFactory.unstructuredLog();
    
    @Pointcut("@annotation(annotation.XXXInterface)")
    public void pointCut() {}
    
    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Exception {
        XXXResponseDTO result;
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //获取目标对象
        Object target = joinPoint.getTarget();
        //获取目标方法
        Method method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        //获取方法注解
        XXXInterface annotation = method.getAnnotation(XXXInterface .class);
        //获取方法参数
        Object[] parameterValues = joinPoint.getArgs();
        XXXRequestDTO request = (XXXRequestDTO) Arrays.stream(parameterValues)
                //过滤继承XXXRequestDTO的类
                .filter(XXXRequestDTO.class::isInstance)
                .findFirst()
                .orElse(null);
                //获取请求id
        String requestId = Objects.isNull(request) ? null : request.getRequestId();
        long start = System.currentTimeMillis();
        try {
        //方法执行前打印日志
            xxx_LOG.info("received request, id: {}, method: {}, info: {}", Objects.isNull(requestId) ? StringUtils.EMPTY : requestId, method.getName(), request);
            //执行目标方法
            result = (BaseResponseDTO) joinPoint.proceed();
        } catch (XXXException pme) {
          
       
        } catch (Throwable e) {
       e.getMessage(), requestId);
        }
        result.setRequestId(requestId);
        result.setResponseDateTime(new Date());
        result.setResponseStatus(ResponseStatusEnum.SUCCESS);
        if (Objects.isNull(result.getResults())) {
            xxx_LOG.info("success handling request, id: {}, cost: {}", Objects.isNull(requestId) ? StringUtils.EMPTY : requestId, System.currentTimeMillis() - start);
        } else {
            int resCount = 0;
            if (result.getResults() instanceof List) {
                resCount = ( (List) result.getResults()).size();
            } else if (result.getResults() instanceof Map) {
                resCount = ( (Map) result.getResults()).size();
            }
            //方法执行后打印日志
            xxx_LOG.info();
        }
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  • 相关阅读:
    安装 Windows 7 VM虚拟机
    qml信号处理
    C++智能指针
    Android自定义控件(二) Android下聚光灯实现
    关于eclipse导入项目出现红色叉或者红色感叹号的各种处理方法
    PHP功能实现小练习题
    基于深度学习的农作物叶片病害检测系统(UI界面+YOLOv5+训练数据集)
    人工神经网络是模拟什么进行信息处理的一种数学模型
    gif图片裁切、压缩导出无水印图片(保姆级教程,亲测可用)
    Redis学习之Redis概述及原理、基本操作及持久化
  • 原文地址:https://blog.csdn.net/L__MY/article/details/133769812