小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

精盡Spring MVC源碼分析 - HandlerMapping 組件(二)之 HandlerInterceptor 攔截器

 行者花雕 2021-06-15

該系列文檔是本人在學(xué)習(xí) Spring MVC 的源碼過程中總結(jié)下來的,可能對讀者不太友好,請結(jié)合我的源碼注釋 Spring MVC 源碼分析 GitHub 地址 進行閱讀

Spring 版本:5.1.14.RELEASE

該系列其他文檔請查看:《精盡 Spring MVC 源碼分析 - 文章導(dǎo)讀》

HandlerMapping 組件

HandlerMapping 組件,請求的處理器匹配器,負責(zé)為請求找到合適的 HandlerExecutionChain 處理器執(zhí)行鏈,包含處理器(handler)和攔截器們(interceptors

  • handler 處理器是 Object 類型,可以將其理解成 HandlerMethod 對象(例如我們使用最多的 @RequestMapping 注解所標注的方法會解析成該對象),包含了方法的所有信息,通過該對象能夠執(zhí)行該方法

  • HandlerInterceptor 攔截器對處理請求進行增強處理,可用于在執(zhí)行方法前、成功執(zhí)行方法后、處理完成后進行一些邏輯處理

由于 HandlerMapping 組件涉及到的內(nèi)容比較多,考慮到內(nèi)容的排版,所以將這部分內(nèi)容拆分成了四個模塊,依次進行分析:

HandlerMapping 組件(二)之 HandlerInterceptor 攔截器

在上一篇《HandlerMapping 組件(一)之 AbstractHandlerMapping》文檔中分析了 HandlerMapping 組件的 AbstractHandlerMapping 抽象類,在獲取HandlerExecutionChain 處理器執(zhí)行鏈時,會去尋找匹配的 HandlerInterceptor 攔截器們,并添加到其中。那么本文將分享 Spring MVC 的攔截器相關(guān)內(nèi)容

HandlerInterceptor

org.springframework.web.servlet.HandlerInterceptor,處理器攔截器接口,代碼如下:

public interface HandlerInterceptor {
/**
 * 前置處理,在 {@link HandlerAdapter#handle(HttpServletRequest, HttpServletResponse, Object)} 執(zhí)行之前
 */
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {

return true;
}

/**
 * 后置處理,在 {@link HandlerAdapter#handle(HttpServletRequest, HttpServletResponse, Object)} 執(zhí)行成功之后
 */
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
}

/**
 * 完成處理,在 {@link HandlerAdapter#handle(HttpServletRequest, HttpServletResponse, Object)} 執(zhí)行之后(無論成功還是失?。? * 條件:執(zhí)行 {@link #preHandle(HttpServletRequest, HttpServletResponse, Object)} 成功的攔截器才會執(zhí)行該方法
 */
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
}
}

HandlerExecutionChain

org.springframework.web.servlet.HandlerExecutionChain,處理器執(zhí)行鏈,也就是通過 HandlerMapping 組件為請求找到的處理對象,包含處理器(handler)和攔截器們(interceptors

構(gòu)造方法

public class HandlerExecutionChain {
/**
 * 處理器
 */
private final Object handler;

/**
 * 攔截器數(shù)組
 */
@Nullable
private HandlerInterceptor[] interceptors;

/**
 * 攔截器數(shù)組。
 *
 * 在實際使用時,會調(diào)用 {@link #getInterceptors()} 方法,初始化到 {@link #interceptors} 中
 */
@Nullable
private List<HandlerInterceptor> interceptorList;

/**
 * 已成功執(zhí)行 {@link HandlerInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)} 的位置
 *
 * 在 {@link #applyPostHandle} 和 {@link #triggerAfterCompletion} 方法中需要用到,用于倒序執(zhí)行攔截器的方法
 */
private int interceptorIndex = -1;

public HandlerExecutionChain(Object handler) {
this(handler, (HandlerInterceptor[]) null);
}

public HandlerExecutionChain(Object handler, @Nullable HandlerInterceptor... interceptors) {
if (handler instanceof HandlerExecutionChain) {
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
this.handler = originalChain.getHandler();
this.interceptorList = new ArrayList<>();
// 將原始的 HandlerExecutionChain 的 interceptors 復(fù)制到 this.interceptorList 中
CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);
// 將入?yún)⒌?nbsp;interceptors 合并到 this.interceptorList 中
CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);
} else {
this.handler = handler;
this.interceptors = interceptors;
}
}
}
  • handler:請求對應(yīng)的處理器對象,可以先理解為 HandlerMethod 對象(例如我們常用的 @RequestMapping 注解對應(yīng)的方法會解析成該對象),也就是我們的某個 Method 的所有信息,可以被執(zhí)行

  • interceptors:請求匹配的攔截器數(shù)組

  • interceptorList:請求匹配的攔截器集合,至于為什么要該屬性,我還沒看明白 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多