基本概念
HandlerExecutionChain,即处理处理链。它包含了:
- 一个处理器,即 HandlerMethod
- 多个拦截器,即 HandlerInterceptor
内部构造
来看它的构造函数:
private final Object handler;
private HandlerInterceptor[] interceptors;
private List<HandlerInterceptor> interceptorList;
public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
// 判断处理器类型
if (handler instanceof HandlerExecutionChain) {
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
// 若是 HandlerExecutionChain,则需要取得真正的处理器
this.handler = originalChain.getHandler();
// 将 HandlerExecutionChain 本身的拦截器和入参的拦截器都合并到集合
this.interceptorList = new ArrayList<HandlerInterceptor>();
CollectionUtils.mergeArrayIntoCollection(
originalChain.getInterceptors(),this.interceptorList);
CollectionUtils.mergeArrayIntoCollection(
interceptors, this.interceptorList);
}else {
this.handler = handler;
this.interceptors = interceptors;
}
}
观察代码, HandlerExecutionChain 包含了处理器和拦截器,但存在处理器本身也是 HandlerExecutionChain ,若是出现这样情况,则必须:
- 提取真正的处理器
- 合并拦截器