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!
Spring AOP之异常转换的更多相关文章
- Spring aop 实现异常拦截
使用aop异常挂载功能可以统一处理方法抛出的异常,减少很多重复代码,实现如下: 1.实现ThrowAdvice public class ExceptionHandler implements Thr ...
- Spring AOP环绕异常影响的报错
最近遇到一个问题,异常是: java.lang.ClassCastException: org.springframework.http.ResponseEntity cannot be cast t ...
- Spring——AOP配置时的jar包异常
首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...
- Spring AOP异常捕获原理
Spring AOP异常捕获原理: 被拦截的方法,须显式的抛出异常,且不能做任何处理, 这样AOP才能捕获到方法中的异常,进而进行回滚. 换句话说,就是在Service层的 ...
- Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...
- Spring AOP声明式事务异常回滚(转)
转:http://hi.baidu.com/iduany/item/20f8f8ed24e1dec5bbf37df7 Spring AOP声明式事务异常回滚 近日测试用例,发现这样一个现象:在业务代码 ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- Spring AOP操作action时无法注入,报NullPointer异常
Spring AOP操作action时无法注入,报NullPointer异常当使用Spring AOP对action层进行操作时,会出现注入失败的问题,出现空指针异常.原因是一般struts2+spr ...
- Spring AOP详解 、 JDK动态代理、CGLib动态代理
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...
随机推荐
- Python核心编程(第八章)--条件和循环
如果一个复合语句(if子句,while或for循环)的代码仅仅包含一行代码,可以和前面的语句写在同一行上: elif语句(else-if) 条件表达式(三元操作符) X if C else Y 计 ...
- [Python 3.x 官方文档翻译]Whetting Your Appetite 欢迎您的使用
If you do much work on computers, eventually you find that there’s some task you’d like to automate. ...
- C程序设计语言练习题1-1
练习1-1 在你自己的系统中运行"hello, world"程序.再有意去掉程序中的部分内容,看看会得到什么出错信息. 代码如下: #include <stdio.h> ...
- qt-vs-addin:Qt4和Qt5之VS插件如何共存与使用
原则上,两者是不可以同时存在的,但是如果都安装了,该如何分别使用他们呢? Qt4 Visual Studio Add-in:官网可以下载安装程序,qt-vs-addin-1.1.11-opensour ...
- windows 挂载linux nfs
windwos挂载linux主机NFS 启动windos NFS客户端服务: 1. 打开控制面板->程序->打开或关闭windows功能->NFS客户端 勾选NFS客户端,即开启wi ...
- virsh 基于xml create VMs虚机
- 支持多文件上传,预览,拖拽,基于bootstra的上传插件fileinput 的ajax异步上传
首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...
- Android 如何检测一个服务是否还在运行?
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- js+css实现模态层效果
在做web前端的时候,有些时候会涉及到模态层,在此提供一种实现思路.希望对大家实用.先贴效果吧: 模态层效果 以下说说在写模态层的时候的思路:通过可配置的參数width,height,title以及c ...
- leetcode_question_57 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...