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!