Spring-AOP之异常转换

引子

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

最原始代码

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

 @Overridepublic 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,提取出一个公共类来处理

@Overridepublic 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>

业务代码:

  @Overridepublic 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!

转载于:https://www.cnblogs.com/yinxiangnan/p/4718543.html

Spring AOP之异常转换相关推荐

  1. Spring aop 实现异常拦截

    使用aop异常挂载功能可以统一处理方法抛出的异常,减少很多重复代码,实现如下: 1.实现ThrowAdvice 1 public class ExceptionHandler implements T ...

  2. 利用spring aop统一处理异常和打日志

    利用spring aop统一处理异常和打日志 spring aop的概念,很早就写博客介绍了,现在在工作中真正使用. 我们很容易写出的代码 我们很容易写出带有很多try catch 和 logger. ...

  3. Spring——AOP配置时的jar包异常

    Spring--AOP配置时的jar包异常 参考文章: (1)Spring--AOP配置时的jar包异常 (2)https://www.cnblogs.com/dyllove98/p/3180193. ...

  4. Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

    AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...

  5. spring Aop实现身份验证和springboot异常统一处理

    文章有不当之处,欢迎指正,如果喜欢微信阅读,你也可以关注我的微信公众号:好好学java,获取优质学习资源. 一.spring Aop身份验证 一般,如果用户没有登录的话,用户只可以查看商品,但是其他的 ...

  6. gtw-050090|执行拦截器时发生异常_执行流程 | 你真的了解Spring AOP的执行顺序吗?...

    Hi! 我是小小,我们又见面了,今天的主要内容是,你真的了解Spring AOP的执行顺序吗?跟随着我的脚步,一块丈量世界,了解世界,重新认识,重新了解Spring AOP的执行顺序. 聊一聊毕业四个 ...

  7. spring aop如何在切面类中获取切入点相关方法的参数、方法名、返回值、异常等信息

    aop思想可以很好的帮助我们实现代码的解耦,比如我们之前提到的,将日志代码与业务层代码完全独立,通过spring aop的代理类进行整合.在切面类中,我们也能够通过spring提供的接口,很好的获取原 ...

  8. Spring AOP 实现业务和异常日志记录实战

    1 业务需求:今日,公司要求对操作的业务和日志统一做处理,需要把业务表数据相关信息存入日志表中,比如表名,方法名,业务id,操作操作时间modifyTIme等等. 除了在业务主动插入日志数据之外,有个 ...

  9. Spring Aop的应用

    2019独角兽企业重金招聘Python工程师标准>>> AOP的基本概念 连接点( Jointpoint) : 表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行 ...

最新文章

  1. HTML 5中SEO可以用那些代码来做优化
  2. execSQL()方法和rawQuery()方法
  3. python简单还是c简单_Python与C的简单比较(Python3.0)
  4. poj 1474 Video Surveillance - 求多边形有没有核
  5. jfinal linux 乱码,分享JFinal中renderFile方法中文文件名乱码问题的解决办法
  6. 学python看谁的视频比较好-Python入门视频看哪个好?适合初学者的教学视频推荐...
  7. include_fns.php_一步一步教你用PHP+MySql筹建网站 No.3 管理页面_mysql
  8. Redis中的代理Sharding
  9. ebook_[EBOOK]十大Java性能问题
  10. 前端学习(2235):react的列表渲染
  11. github注册账号一直验证失败
  12. linux7重装linux6,CentOS6远程重装7过程
  13. 2017年12月20日 内置对象
  14. Correlation coefficients and appliction in fMRI Data
  15. 微软windows10易升_微软正式发布Win10的2020年5月更新
  16. 开源BI工具对比(三) DataEase
  17. Dukto-R6超级实用的免费跨平台局域网文件/文件夹传输工具
  18. MyBatis整合Spring的实现(7)
  19. 如何删除 Windows 10 上的 Windows.old 文件夹?
  20. fluent周期边界_在Fluent中采用TUI设置周期性边界的方法

热门文章

  1. 最大并发连接数和最大会话数的区别
  2. EZ 2018 03 23 NOIP2018 模拟赛(五)
  3. 有什么值得推荐的Java Web练手项目?
  4. PHP易混淆函数的区分
  5. MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】...
  6. WPF:ListView数据绑定及Style
  7. Windows SDK 非模态对话框的消息处理
  8. 在SharePoint 2010系统中安装RBS FILESTREAM Provider
  9. 腾讯工作心得:原型该画到什么程度?
  10. Core Linux折腾(二)