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!

  • 相关阅读:
    转载:从51CTO转来的两篇关于SQL的文章
    转载:几万年前,有一只猴子大闹地府后删库跑路...
    【java/oralce/sql】往一张仅有id,名称,创建时间三个字段的表中插入百万数据需要多久?1分26秒
    处处留心皆学问
    [oracle/java/sql]用于上十万批量数据插入Oracle表的Java程序
    Linux学习_003_虚拟机CentOS 7.5 如何固定IP地址
    Linux学习_002_VMware12.0 Pro 中安装 CentOS-7.5(桌面版)
    Linux学习_001_VMware10.0 && VMware12.0 Pro && VMware14.0 Pro && VMware 15.0 Pro 的安装与破解
    day76_淘淘商城项目_09_商品详情页动态展示实现(jsp+redis) + FreeMarker模板引擎入门 + 商品详情页静态化实现(Win版本的nginx作http服务器)_匠心笔记
    Eclipse注释模板设置详解
  • 原文地址:https://www.cnblogs.com/yinxiangnan/p/4718543.html
Copyright © 2011-2022 走看看