zoukankan      html  css  js  c++  java
  • AOP实现的统一日志处理

    编写一个切面类

    package com.**.sale.score.service.aspect;
    
    import com.zhangmen.sale.score.common.Result;
    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.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.dao.DuplicateKeyException;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class ExceptionAspect {
    
        private static final Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);
    
        @Pointcut("execution(* com.*.sale.score.service.impl.*Impl.*(..))")
        public void pointcut() {
        }
    
        @Around("pointcut()")
        public Object interceptor(ProceedingJoinPoint proceedingJoinPoint) {
            MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
            String className = methodSignature.getDeclaringTypeName();
            String methodName = methodSignature.getName();
            Object returnObject;
            try {
                returnObject = proceedingJoinPoint.proceed();
            } catch (Exception e) {
                if (e instanceof DuplicateKeyException) {
                    logger.error("call {},系统异常-DuplicateKeyException异常:{}", methodName, e);
                    throw (DuplicateKeyException) e;
                }
                if (e instanceof ArithmeticException) {
                    logger.error("call {},系统异常-ArithmeticException异常:{}", methodName, e);
                    throw (ArithmeticException) e;
                }
                if (e instanceof BusinessException) {
                    logger.error("call {},自定义异常-BusinessException异常:{}", methodName, e);
                    return Result.failure(e.getMessage());
                }
                logger.error("call {},系统异常-其他异常:{}", methodName, e);
                throw new RuntimeException(e);
            } catch (Throwable e) {
                logger.error("call {},系统异常:{}", methodName, e);
                throw new RuntimeException(e);
            }
            return returnObject;
    
        }
    
    
    
    }

    可以捕获自己定义的异常

    package com.**.sale.score.service.aspect;/**
    
    /**
     *@ClassName BusinessException
     *@Description 自定义异常,全局捕获
     *@Author wangwei
     *@Date 2019/10/16 10:27
     *@Version V1.0
     **/
    public class BusinessException extends RuntimeException  {
        public BusinessException(String message) {
            super(message);
        }
    }
  • 相关阅读:
    我大学时代的好朋友要结婚了!
    (function(root,factory){})(this,function($){}) 一个立即执行的匿名函数自调
    非书面的非官方的常见的HTTP请求状态码分析
    一个页面多个HTTP请求 页面卡顿!
    requestAnimationFrame Web中写动画的另一种选择
    js 点击 返回顶部 动画
    css3动画 一行字鼠标触发 hover 从左到右颜色渐变
    var 和 let 的异同?
    H5案例分享:使用JS判断客户端、浏览器、操作系统类型
    基于浏览器的HTML5地理定位
  • 原文地址:https://www.cnblogs.com/UncleWang001/p/12972562.html
Copyright © 2011-2022 走看看