在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了。。