日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring AOP之异常转换

發布時間:2025/3/20 javascript 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring AOP之异常转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

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之异常转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。