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之异常转换的更多相关文章

  1. Spring aop 实现异常拦截

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

  2. Spring AOP环绕异常影响的报错

    最近遇到一个问题,异常是: java.lang.ClassCastException: org.springframework.http.ResponseEntity cannot be cast t ...

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

    首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...

  4. Spring AOP异常捕获原理

    Spring AOP异常捕获原理:        被拦截的方法,须显式的抛出异常,且不能做任何处理, 这样AOP才能捕获到方法中的异常,进而进行回滚.        换句话说,就是在Service层的 ...

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

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

  6. Spring AOP声明式事务异常回滚(转)

    转:http://hi.baidu.com/iduany/item/20f8f8ed24e1dec5bbf37df7 Spring AOP声明式事务异常回滚 近日测试用例,发现这样一个现象:在业务代码 ...

  7. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  8. Spring AOP操作action时无法注入,报NullPointer异常

    Spring AOP操作action时无法注入,报NullPointer异常当使用Spring AOP对action层进行操作时,会出现注入失败的问题,出现空指针异常.原因是一般struts2+spr ...

  9. Spring AOP详解 、 JDK动态代理、CGLib动态代理

    AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...

随机推荐

  1. n阶行列式计算----c语言实现(完结)

    花了半天时间,写了这个n阶行列式计算的程序,应该算是比较优美吧,有很多地方多次做了优化,程序占用内存不是很大,要是说小吧,也不合适,因为里边有一个递归,而且递归的深度还比较深.时间复杂度具体没有细看, ...

  2. Python新手学习基础之循环语句——While循环

    while循环 上一节的条件语句实际上只能执行一次,如果要反复的判断执行一些事件要怎么办? 这个时候就需要靠while.for等循环语句了. 我们先来认识下while循环,何为while循环?就是在某 ...

  3. C程序设计语言练习题1-11

    练习1-11 你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢? 代码如下: #include <stdio.h> // 包含标准库的信息. #de ...

  4. 关于js事件冒泡和时间捕获

    (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...

  5. POJ 3384 Feng Shui

    http://poj.org/problem?id=3384 题意:给一个凸包,求往里面放两个圆(可重叠)的最大面积时的两个圆心坐标. 思路:先把凸包边往内推R,做半平面交,然后做旋转卡壳,此时得到最 ...

  6. Altium Designer规则

    1.PCB规则 是PCB设计中至关重要的一个环节:保证PCB符合电气要求.机械加工(精度)要求:为自动布局.布线和部分手动布局.布线操作提供依据 为规则检查提供依据,PCB编辑期间,AD会实时地进行一 ...

  7. VS IDE环境下,windows GUI(Qt MFC,win32)使用控制台实时打印调试信息

    在工程属性的页面下,点击Build Events,在Build Events下点击Post-Build Event. 然后再Command Line里面输入以下命令: editbin /SUBSYST ...

  8. PHP常见报错解析

    {错误类型}: {错误原因} in {错误文件} on {错误行数} 说明了在哪个文件的哪一行中因何种原因出现了何种错误. 常见的错误类型一般有下面几种: Parse error(解析错误)一般都伴随 ...

  9. javascript对URL中的参数进行简单加密处理

    javascript的api本来就支持Base64,因此我们可以很方便的来进行编码和解码. var encodeData = window.btoa("name=xiaoming&a ...

  10. 应用Observer接口实践Observer模式

    原文:http://zhangjunhd.blog.51cto.com/113473/68949/ 在Java中通过Observable类和Observer接口实现了观察者模式.Observer对象是 ...