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将这些分散在各个业务逻辑中的代码 ...
随机推荐
- test在博客中嵌入实例代码
// 其实很简单,只要把css.div.js代码直接写在里面就可以了.通过查看源代码就能看得很清楚了. 要注意的一点就是:如果div里没有任何内容,则它会被(博客网)删除掉,所以即使没有内容, ...
- PHP+MySQL Smarty简单分页显示示例
一.分页程序的原理 分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page). 有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在m ...
- java中setDate(Date date)方法和String与Date之间的转换
经常在开发的过程中遇到这样的问题,从数据库中读出来的数据需要转换为对像或者java bean,此时经常使用到setDate(Date date);这样的方法.感觉这是个很简单而又难受的事情,在这里浪费 ...
- injector
angular 提供了一套依赖注入的机制,和后台很像.虽然我不觉得有很重要. var $injector = angular.injector(["myModule"]); var ...
- /sbin/ifup: configuration for eth0 not found解决
/sbin/ifup: configuration for eth0 not found. Usage: ifup <device name> 那么应该是在/etc/sysconfig/n ...
- 【转】ubuntu12.04完美安装QQ2012、QQMusic、Foxmail等--wine
原文网址:http://blog.csdn.net/hanmengaidudu/article/details/17616921 其实在这之前,试过无数次的wine模拟,没有一次成功的,也不能说是不成 ...
- 多重背包问题的两种O(M*N)解法
多重背包的题目很多,最著名的是poj1742楼教主的男人八题之一. poj1742:coins 有几种面值的钱币和每种的数量,问能够组成m以内的多少种钱数 这个题大家都归为多重背包问题,不过跟实际意义 ...
- CCF软考---《有趣的数》
脑子一热报了CCF的软测..但是又觉得好像并没有什么卵用,就当为蓝桥杯预热然后顺便去软件学院玩一玩吧,遇到一个有意思的题: time limits : 1s 问题描述 我们把一个数称为有趣的,当且仅当 ...
- table表格边框样式
; border-left:1px solid #aaa; border-top:1px solid #aaa; } td{border-right:1px solid #aaa; border-bo ...
- iOS 提示音播放
首先找到对应的素材 音频文件 写一个类继承 NSObject 命名为AudioUtil 导入支撑文件 #import <AVFoundation/AVFoundation.h> #impo ...