zoukankan      html  css  js  c++  java
  • Spring AOP之异常转换

     

    Spring-AOP之异常转换

    引子

    最近项目遇到了一个问题,就是说业务层向展现层需要转换成统一个异常类,并抛出异常,但是由于业务层的异常类过多,所以导致业务异常转换代码充斥着异常转换的代码,本着程序猿能省写代码就省写代码的原则,决定用Spring AOP来做一个切片,业务异常类转换.

    最原始代码

    最原始的代码,咱简称V1.0

     @Override
        public GnAreaVo selectByID(GnAreaCondition condition) throws CallerException {
            try {
                //业务处理
                if (StringUtils.isEmpty(condition.getAreaCode()))
                    throw new BusinessException("10001", "区域编码不能为空");
                Gson gson = new Gson();
                //处理结果
                return gson.fromJson(gson.toJson(iGnAreaBusinessService.selectByID(condition.getAreaCode())), GnAreaVo.class);
            } catch (BusinessException ex) {
                //
                throw new CallerException("100001", ex.getErrorMessage());
            } catch (SystemException ex) {
                //
                throw new CallerException("100001", ex.getMessage());
            } catch (Exception ex) {
                //
                throw new CallerException("10001", "系统异常");
            }
        }
    

    升级版本

    升级版本,简称V1.1,提取出一个公共类来处理

    @Override
        public GnAreaVo selectByID(GnAreaCondition condition) throws CallerException {
            try {
                //业务处理
                if (StringUtils.isEmpty(condition.getAreaCode()))
                    throw new BusinessException("10001", "区域编码不能为空");
                Gson gson = new Gson();
                //处理结果
                return gson.fromJson(gson.toJson(iGnAreaBusinessService.selectByID(condition.getAreaCode())), GnAreaVo.class);
            } catch (BusinessException ex) {
                //
                throw DubboExceptAssembler.assemble(ex);
            } catch (SystemException ex) {
                //
                throw DubboExceptAssembler.assemble(ex);
            } catch (Exception ex) {
                //
                throw DubboExceptAssembler.assemble(ex);
            }
        }
    

    最终版

    代码更加简单了,并且能支持更加多异常类的转换,减少业务程序的无用代码,下面来看看怎么实现的。
    首先写一个AOP

    import com.ai.runner.base.exception.BusinessException;
    import com.ai.runner.base.exception.SystemException;
    import com.ai.runner.utils.util.DubboExceptAssembler;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.aspectj.lang.JoinPoint;
    
    public class DubboExceptionConvertInterceptor {
    
        private static final Logger logger = LogManager.getLogger(DubboExceptionConvertInterceptor.class);
    
        public void convertException(JoinPoint joinPoint, Exception error) {
            logger.error("执行{}类中的{}方法出错,出错原因:{}", joinPoint.getTarget().getClass().getName(),
                    joinPoint.getSignature().getName());
            if (error instanceof SystemException) {
                throw DubboExceptAssembler.assemble((SystemException) error);
            }
            if (error instanceof BusinessException) {
                throw DubboExceptAssembler.assemble((BusinessException) error);
            }
            throw DubboExceptAssembler.assemble(error);
        }
    }
    

    Spring的配置:

    <bean id="dubboExceptionConvertor" class="DubboExceptionConvertInterceptor"/>
    
        <aop:config>
            <aop:aspect id="aspectLoggging" ref="dubboExceptionConvertor">
                <aop:pointcut id="dubboExceptionThrowing"
                              expression="execution (* com.ai.runner.center.common.api.*.impl.*.*(..))"/>
                <aop:after-throwing method="convertException" throwing="error"
                                    pointcut-ref="dubboExceptionThrowing"/>
            </aop:aspect>
        </aop:config>
    

    业务代码:

      @Override
        public GnAreaVo selectByID(GnAreaCondition condition) throws CallerException {
            if (StringUtils.isEmpty(condition.getAreaCode()))
                throw new BusinessException("10001", "区域编码不能为空");
            Gson gson = new Gson();
            return gson.fromJson(gson.toJson(iGnAreaBusinessService.selectByID(condition.getAreaCode())), GnAreaVo.class);
        }
    

    Done!

  • 相关阅读:
    【转】PHP foreach 小结
    【转】div,p,span,ul,li,dl,dt,dd,a,img,h,strong,em用法
    【转】网页制作中的CSS+DIV:dl,dt,dd分别表示什么意思啊?请说明啊,谢谢有什么功能?
    【转】[教程] CSS入门3:如何插入CSS样式
    PHP输出多个空格
    【转】浅谈HTTP中Get与Post的区别
    HDOJ 2433 Counter Strike 逆序对 线段树
    hdu 4193 Nonnegative Partial Sums 单调队列
    vijos 1750 建房子 单调队列
    poj 2155 Matrix 二维树状数组
  • 原文地址:https://www.cnblogs.com/yinxiangnan/p/4718543.html
Copyright © 2011-2022 走看看