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. 更好的使用chrome

    Ctrl+tab           在标签页之间切换 Ctrl+1              到 Ctrl+8 切换到指定位置编号的标签页.您按下的数字代表标签页横条上的相应标签位置 Ctrl+9 ...

  2. dedecms 织梦ping服务插件 最新破解可用版

    dedecms 织梦ping服务插件 最新破解可用版  ping_gbk.xml <module> <baseinfo> name=ping服务 team=井哥 time=20 ...

  3. Map的三种遍历

    import java.util.*;/*** Map的三种遍历方式* @author Administrator**/public class m {public static void main( ...

  4. Android开发的体会

    View Functionality--------------->逻辑 Data----------------------->实体,包括从View.Net.其他对象比如Location ...

  5. 关于Python的3张图

  6. windows环境下nutch2.x 在eclipse中实现抓取数据存进mysql详细步骤

    nutch2.x 在eclipse中实现抓取数据存进mysql步骤 最近在研究nutch,花了几天时间,也遇到很多问题,最终结果还是成功了,在此记录,并给其他有兴趣的人提供参考,共同进步. 对nutc ...

  7. Apache+php配置 Mysql安装出错解决办法

    此文包括的注意内容:软件版本及下载地址Apache2.4的配置和安装php7.0的配置mysql5.5的安装常见问题及解决方法1.软件版本Windows server 2008 r2+ 64位Apac ...

  8. javascript笔记7之对象数组

    /* var box = new Array(); //声明一个数组,空数组 alert(typeof box); //数组属于object类型 var box = new Array('李炎恢', ...

  9. phpcms:七、list.html

    1.列表页{pc:content action="lists" catid="$catid" num="25" order="id ...

  10. wpf新增记录时用多线程的问题

    多线程虽然可以增加用户操作体验,但是有时候会出现意想不到的错误. 如果采用分布式,数据库在另外服务器上,当网络出现问题,或者数据库繁忙,那么新增数据就会等待,这时候用户如果以为没有操作,而多次点击新增 ...