使用SpringAOP做自定义异常处理

使用SpringAOP做自定义异常处理

1.自定义一个注解,该注解需要在各个项目的启动类上加上

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import({ExceptionHandle.class, ServiceAspect.class})
public @interface EnableExceptionAdvice {

}

2.自定义注解

3.Spring的自定义异常处理

在这里插入图片描述

4.自定义异常注解

在这里插入图片描述

  1. 自定义的异常只可以抛RuntimeException
@Component
@Aspect
@Slf4j
public class ServiceAspect {
    //修改一下对方法的检测,这样也可以检测使用了MybatisPlus的IService注解的类的所有方法,+代表实现类
    @Around("within(@org.springframework.stereotype.Service *) || within(com.baomidou.mybatisplus.extension.service.IService+)")
    public Object handleExceptionLog(ProceedingJoinPoint pj){
        try {
            //执行方法前进行一次调用
            long beforeMethod = System.currentTimeMillis();
            log.debug("{}方法执行了,在{}时间执行了",pj.getSignature().getName(),beforeMethod);
            log.debug("{}方法执行了,参数:{}",pj.getSignature().getName(), Arrays.asList(pj.getArgs()));
            //执行方法
            Object result = pj.proceed();
            //正常执行方法后检测一次
            log.debug("{}方法执行成功了,耗时{}秒",pj.getSignature().getName(),System.currentTimeMillis()-beforeMethod);
            return result;
        } catch (Throwable throwable) {
            //抛异常进行检测
            log.error("{}方法执行失败,原因:{}",pj.getSignature().getName(),throwable.getMessage(),throwable );
            //对这个异常进行判断
            if (throwable instanceof LyException){
                throw (LyException)throwable;
            }
            throw new LyException(500,throwable );
        }
    }
}

5.AOP的环绕通知

在这里插入图片描述

  1. 使用切面对Spring的Service注解进行切面

6.在其他项目的启动类中加入该注解

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring AOP时,我们需要遵循以下步骤: 1. 引入Spring AOP依赖。在pom.xml中添加相应的依赖项。 2. 创建一个切面类,该类使用注解来标识需要进行切面处理的方法。可以使用定义的注解来标记需要异常捕获的方法,如中的@ExceptionCatch注解。 3. 在Spring配置文件中配置AOP。通过配置文件或者使用注解的方式将切面类和目标类进行关联。 4. 运行应用程序。当目标方法被调用时,AOP将会在特定的切点处执行切面逻辑。 例如,在Spring Boot中使用Spring AOP,可以按照以下步骤进行配置: 1. 引入Spring AOP依赖。在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建一个切面类,例如ExceptionCatchAspect.java,使用@Aspect注解标记该类为切面类。在切面类中定义切点和通知,可以使用定义的注解来标记需要异常捕获的方法。 ```java @Aspect @Component public class ExceptionCatchAspect { @Pointcut("@annotation(com.example.ExceptionCatch)") public void exceptionCatchPointcut() {} @AfterThrowing(pointcut = "exceptionCatchPointcut()", throwing = "ex") public void afterThrowingAdvice(Exception ex) { // 在这里编写异常捕获的逻辑 // 可以打印方法名、参数等信息,方便排查问题 } } ``` 3. 在Spring配置文件中配置AOP。可以通过在启动类上添加@EnableAspectJAutoProxy注解来启用AOP,或者在配置文件中进行配置。例如在application.properties文件中添加以下配置: ``` spring.aop.auto=true ``` 4. 运行应用程序。当带有@ExceptionCatch注解的方法被调用且抛出异常时,AOP将会执行切面类中定义异常捕获逻辑。 请注意,以上只是一个简单的使用Spring AOP的示例,实际使用中可能会涉及更多的配置和细节。具体的实现方式还取决于你使用的Spring版本和项目的具体需求。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [使用SpringAOP来捕获异常](https://blog.csdn.net/a_c_c_a/article/details/106061934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [使用Spring AOP修改请求、返回参数](https://blog.csdn.net/weixin_33785108/article/details/91911138)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值