@RestControllerAdvice注解全局异常处理的原理

在spring容器初始化的时候,会将加了@RestControllerAdvice的类装载到容器中,同时调用

initExceptionHandlerAdviceCache方法,该方法内部初始化了一个exceptionHandlerAdviceCache
private final Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> exceptionHandlerAdviceCache = new LinkedHashMap();

//exceptionHandlerAdviceCache的定义,该对象是一个HashMap

 也就是说,spring容器初始化的时候,会将所有加了@RestControllerAdvice的bean作为key,根据adviceBean的类型创建出一个ExceptionHandlerMethodResolver对象,将这个ExceptionHandlerMethodResolver对象作为value存到缓存中。

//获取所有的adviceBean
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(this.getApplicationContext());
ControllerAdviceBean adviceBean = (ControllerAdviceBean)var2.next();
Class<?> beanType = adviceBean.getBeanType();

//创建methodResolver对象

ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);

//放入缓存

this.exceptionHandlerAdviceCache.put(adviceBean, resolver);

 在之后,当controller定义的handler抛出了自定义的Advice对应处理的Exception类的时候,

@RestControllerAdvice
public class GolbalAdvice {
    //对应处理MyException类型的异常
    @ExceptionHandler({MyException.class})
    public String fail(){
//        return SysResult.fail();
        return "请求失败";
    }
}

spring会找到exceptionHandlerAdviceCache中对每个entry进行匹配,看其是否能处理controller中抛出的异常

//遍历adviceAche缓存中的entry
Iterator var9 = this.exceptionHandlerAdviceCache.entrySet().iterator();
//判断每个advice是否能处理controller中handler抛出的异常
if (advice.isApplicableToBeanType(handlerType))

这里注意,有两种我们常用的异常处理的方式,其实对应着ExceptionHandlerExceptionResolver的两个属性:

exceptionHandlerCache对应着针对与controller控制层的异常处理方法
exceptionHandlerAdviceCache对应着全局的异常处理方法
exceptionLookupCache对应着已经查询过(处理过的)异常类型

 而exceptionLookupCache缓存,顾名思义,就是加在spring容器上的一层缓存,

public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionType) {
        //从缓存看有没有查询过处理过这个异常类型
        Method method = (Method)this.exceptionLookupCache.get(exceptionType);
        if (method == null) {
            method = this.getMappedMethod(exceptionType);
            //放进缓存,下次再次出现这类异常,直接从缓存中返回
            this.exceptionLookupCache.put(exceptionType, method);
        }

        return method;
    }

如果没有这层缓存,在每一次处理异常的时候,都要去advicebena下面所有的方法里遍历一遍找到对应的处理方法。

问题2:exceptionHandlerAdviceCache是何时被初始化的?

断点跟踪:在WebMvcConfigurationSupport中的名字为handlerExceptionResolver的bean对象中,对所有的异常处理的bean进行了初始化,

@Bean
    public HandlerExceptionResolver handlerExceptionResolver(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager) {
        List<HandlerExceptionResolver> exceptionResolvers = new ArrayList();
        //.....
        if (exceptionResolvers.isEmpty()) {
            //添加异常处理器
            this.addDefaultHandlerExceptionResolvers(exceptionResolvers, contentNegotiationManager);
        }

         //....
    }

然后点着addDefaultHandlerExceptionResolvers这个方法一直点进去最终在initExceptionHandlerAdviceCache方法中完成了对我们自定义的advicebean的初始化。

那WebMvcConfigurationSupport是什么时候被加载的呢?

 在编译器中点击这个类被装载到了spring容器中的哪个bean对象?

最终看到了EnableWebMvcConfiguration类(spring配置文件中规定的需要加载的bean对象)中的

 EnableWebMvcConfiguration这个bean间接继承了WebMvcConfigurationSupport类,那自然会加载WebMvcConfigurationSupport中的bean了。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_34116044

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值