广告
返回顶部
首页 > 资讯 > 精选 >自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析
  • 104
分享到

自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析

2023-06-15 09:06:51 104人浏览 独家记忆
摘要

这篇文章主要为大家展示了“自定义注解和springaop捕获Service层异常并处理自定义异常的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“自定义注解和springAOP捕获Serv

这篇文章主要为大家展示了“自定义注解和springaop捕获Service层异常并处理自定义异常的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析”这篇文章吧。

一 自定义异常

public class NoParamsException extends Exception {    //用详细信息指定一个异常    public NoParamsException(String message){        super(message);    }    //用指定的详细信息和原因构造一个新的异常    public NoParamsException(String message, Throwable cause){        super(message,cause);    }    //用指定原因构造一个新的异常    public NoParamsException(Throwable cause) {        super(cause);    }}

二 自定义注解

@Documented@Target({ElementType.METHOD, ElementType.TYPE}) //可在类或者方法使用@Retention(RetentionPolicy.RUNTIME)public @interface ServiceExceptionCatch {}

三 注解切面处理类

@Component@Aspect@Slf4jpublic class ServiceExceptionHandler {    @Around("@annotation(com.zhuzher.annotations.ServiceExcepCatch)  || @within(com.zhuzher.annotations.ServiceExcepCatch)")    public ResponseMessage serviceExceptionHandler(ProceedingJoinPoint proceedingJoinPoint) {        ResponseMessage returnMsg;        try {            returnMsg = (ResponseMessage) proceedingJoinPoint.proceed();        } catch (Throwable throwable) {            log.error("ServiceExcepHandler serviceExcepHandler failed", throwable);            //单独处理缺少参数异常            if(throwable instanceof NoParamsException) {                returnMsg = ResponseMessage.failture(ErrorCode.ARG_CAN_NOT_BE_EMPTY);            }else{//其他正常返回                returnMsg=ResponseMessage.newErrorsMessage(throwable.getMessage());            }        }        return returnMsg;    }}

自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析

即可捕获改异常,并自定义处理逻辑!

捕获Service层异常,统一处理

新增注解,实现类和方法层级的异常捕获

package com.ahdruid.aop.annotation;import java.lang.annotation.*; @Documented@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface ServiceExcepCatch {}

异常处理handler

package com.ahdruid.aop;import com.ahdruid.ReturnMsg;import com.ahdruid.errorenums.BaseErrorEnum;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component; @Component@Aspect@Slf4jpublic class ServiceExcepHandler {     @Around("@annotation(com.ahdruid.aop.annotation.ServiceExcepCatch)  || @within(com.ahdruid.aop.annotation.ServiceExcepCatch)")    public ReturnMsg serviceExcepHandler(ProceedingJoinPoint proceedingJoinPoint) {        ReturnMsg returnMsg = new ReturnMsg();        try {            returnMsg = (ReturnMsg) proceedingJoinPoint.proceed();        } catch (Throwable throwable) {            log.error("ServiceExcepHandler serviceExcepHandler failed", throwable);            returnMsg.setError(BaseErrorEnum.SYS_ERROR_UNKNOW);        }        return returnMsg;    }}

使用时,在类或者方法上加上注解@ServiceExcepCatch

以上是“自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: 自定义注解和springAOP捕获Service层异常并处理自定义异常的示例分析

本文链接: https://www.lsjlt.com/news/279388.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作