菜鳥(niǎo)學(xué)習(xí) Spring 之 DispatcherServlet 總覽

寫(xiě)在前面

關(guān)于 DispatcherServlet 已經(jīng)有很多博客,這里可以說(shuō)是一篇整理文章然后加了一些自己的理解。
說(shuō)到 DispatcherServlet 就不得不提到 Servlet,所以下面主要講講 Servlet

Servlet 生命周期

關(guān)于 Servlet 周期,這里結(jié)合源碼注釋進(jìn)行說(shuō)明:

package javax.servlet;

public interface Servlet {

    /**
     * The servlet container calls the <code>init</code> method exactly once
     * after instantiating the servlet. The <code>init</code> method must
     * complete successfully before the servlet can receive any requests.
     * 
     *  大意就是 servlet 容器會(huì)在實(shí)例化時(shí)調(diào)用一次(僅此一次)init 方法
     *  調(diào)用完成后才可以接收請(qǐng)求
     *
     */
    public void init(ServletConfig config) throws ServletException;

    /**
     * Called by the servlet container to allow the servlet to respond to a
     * request.
     *  
     * 
     * This method is only called after the servlet's <code>init()</code> method
     * has completed successfully.
     * 
     *  servlet 容器調(diào)用 service 方法響應(yīng)請(qǐng)求,并且該方法只能在 init 方法調(diào)用成功后才能被調(diào)用。
     * 
     */
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException;


    /**
     * Called by the servlet container to indicate to a servlet that the servlet
     * is being taken out of service. This method is only called once all
     * threads within the servlet's <code>service</code> method have exited or
     * after a timeout period has passed. After the servlet container calls this
     * method, it will not call the <code>service</code> method again on this
     * servlet.
     *
     *  
     * 大意就是說(shuō),這個(gè)方法被調(diào)用后,會(huì)等待容器內(nèi)所有線程執(zhí)行 service 方法完畢
     * 或超時(shí)(并且容器不會(huì)再接收請(qǐng)求調(diào)用 service 方法),才執(zhí)行方法內(nèi)容。
     * 
     */
    public void destroy();
    
    public ServletConfig getServletConfig();

    public String getServletInfo();
}

由源碼可知,Servlet 生命周期:

Servlet 生命周期
前端總控制器模式

前端控制器模式(Front Controller Pattern)是用來(lái)提供一個(gè)集中的請(qǐng)求處理機(jī)制,所有的請(qǐng)求都將由一個(gè)單一的處理程序處理。參考1參考2。

前端總控制器

從圖中可以看出請(qǐng)求進(jìn)來(lái)之后,統(tǒng)一由 FrontController 處理,再委托給相應(yīng)的應(yīng)用控制器,ApplicationController 再調(diào)度獲得結(jié)果。是不是感覺(jué)Spring MVC呼之欲出?

Spring MVC

從前端總控制器模式就能看出點(diǎn)端倪,Spring MVC就是在其之上設(shè)計(jì)的。DispatcherServlet 就是 FrontController,我們經(jīng)常寫(xiě)的 @Controller 等方法則是 ApplicationController,從而完成整個(gè)MVC模式實(shí)現(xiàn)。

DispatcherServlet 運(yùn)行鏈路
DispatcherServlet

類圖可以看到 DispatcherServlet 最后還是實(shí)現(xiàn)了 Servlet 接口,也就是意味著它始終會(huì)遵循 Servlet 運(yùn)行生命周期。

DispatcherServlet init調(diào)用鏈路:

init

DispatcherServlet service調(diào)用鏈路:

service

DispatcherServlet destroy調(diào)用鏈路:

destroy
END

這篇文章只能是一個(gè)總覽,讓自己對(duì) Spring MVC 有個(gè)整體概念。從中我們可以了解其完全在 Servlet 基礎(chǔ)上進(jìn)行設(shè)計(jì),并且盡可能的利用。

最后,期待自己能夠完成這個(gè)系列的學(xué)習(xí)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容