javascript
Spring中DispacherServlet、WebApplicationContext、ServletContext的关系
解釋一:
? ? 要想很好理解這三個上下文的關系,需要先熟悉spring是怎樣在web容器中啟動起來的。spring的啟動過程其實就是其IoC容器的啟動過程,對于web程序,IoC容器啟動過程即是建立上下文的過程。
spring的啟動過程:
首先,對于一個web應用,其部署在web容器中,web容器提供其一個全局的上下文環(huán)境,這個上下文就是ServletContext,其為后面的spring IoC容器提供宿主環(huán)境;
其次,在web.xml中會提供有contextLoaderListener。在web容器啟動時,會觸發(fā)容器初始化事件,此時contextLoaderListener會監(jiān)聽到這個事件,其contextInitialized方法會被調用,在這個方法中,spring會初始化一個啟動上下文,這個上下文被稱為根上下文,即WebApplicationContext,這是一個接口類,確切的說,其實際的實現(xiàn)類是XmlWebApplicationContext。這個就是spring的IoC容器,其對應的Bean定義的配置由web.xml中的context-param標簽指定。在這個IoC容器初始化完畢后,spring以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE為屬性Key,將其存儲到ServletContext中,便于獲取;
再次,contextLoaderListener監(jiān)聽器初始化完畢后,開始初始化web.xml中配置的Servlet,這個servlet可以配置多個,以最常見的DispatcherServlet為例,這個servlet實際上是一個標準的前端控制器,用以轉發(fā)、匹配、處理每個servlet請求。DispatcherServlet上下文在初始化的時候會建立自己的IoC上下文,用以持有spring mvc相關的bean。在建立DispatcherServlet自己的IoC上下文時,會利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先從ServletContext中獲取之前的根上下文(即WebApplicationContext)作為自己上下文的parent上下文。有了這個parent上下文之后,再初始化自己持有的上下文。這個DispatcherServlet初始化自己上下文的工作在其initStrategies方法中可以看到,大概的工作就是初始化處理器映射、視圖解析等。這個servlet自己持有的上下文默認實現(xiàn)類也是mlWebApplicationContext。初始化完畢后,spring以與servlet的名字相關(此處不是簡單的以servlet名為Key,而是通過一些轉換,具體可自行查看源碼)的屬性為屬性Key,也將其存到ServletContext中,以便后續(xù)使用。這樣每個servlet就持有自己的上下文,即擁有自己獨立的bean空間,同時各個servlet共享相同的bean,即根上下文(第2步中初始化的上下文)定義的那些bean。
解釋二:
在Web容器(比如Tomcat)中配置Spring時,你可能已經司空見慣于web.xml文件中的以下配置代碼: [plain]?view plaincopy? ? 以上配置首先會在ContextLoaderListener中通過<context-param>中的applicationContext.xml創(chuàng)建一個ApplicationContext,再將這個ApplicationContext塞到ServletContext里面,通過ServletContext的setAttribute方法達到此目的,在ContextLoaderListener的源代碼中,我們可以看到這樣的代碼:
[java] view plaincopy? ? 以上由ContextLoaderListener創(chuàng)建的ApplicationContext是共享于整個Web應用程序的,而你可能早已經知道,DispatcherServlet會維持一個自己的ApplicationContext,默認會讀取/WEB-INFO/<dispatcherServletName>-servlet.xml文件,而我么也可以重新配置:
[plain] view plaincopy? ? 問題是:以上兩個ApplicationContext的關系是什么,它們的作用作用范圍分別是什么,它們的用途分別是什么?
? ? ContextLoaderListener中創(chuàng)建ApplicationContext主要用于整個Web應用程序需要共享的一些組件,比如DAO,數(shù)據(jù)庫的ConnectionFactory等。而由DispatcherServlet創(chuàng)建的ApplicationContext主要用于和該Servlet相關的一些組件,比如Controller、ViewResovler等。
? ? 對于作用范圍而言,在DispatcherServlet中可以引用由ContextLoaderListener所創(chuàng)建的ApplicationContext,而反過來不行。
? ? 在Spring的具體實現(xiàn)上,這兩個ApplicationContext都是通過ServletContext的setAttribute方法放到ServletContext中的。但是,ContextLoaderListener會先于DispatcherServlet創(chuàng)建ApplicationContext,DispatcherServlet在創(chuàng)建ApplicationContext時會先找到由ContextLoaderListener所創(chuàng)建的ApplicationContext,再將后者的ApplicationContext作為參數(shù)傳給DispatcherServlet的ApplicationContext的setParent()方法,在Spring源代碼中,你可以在FrameServlet.java中找到如下代碼:
wac.setParent(parent);? ? 其中,wac即為由DisptcherServlet創(chuàng)建的ApplicationContext,而parent則為有ContextLoaderListener創(chuàng)建的ApplicationContext。此后,框架又會調用ServletContext的setAttribute()方法將wac加入到ServletContext中。
當Spring在執(zhí)行ApplicationContext的getBean時,如果在自己context中找不到對應的bean,則會在父ApplicationContext中去找。這也解釋了為什么我們可以在DispatcherServlet中獲取到由ContextLoaderListener對應的ApplicationContext中的bean。Spring API中的解釋:
public interface WebApplicationContextextends?ApplicationContextInterface to provide configuration for a web application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
This interface adds a?getServletContext()?method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process.
Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.
In addition to standard application context lifecycle capabilities, WebApplicationContext implementations need to detect?ServletContextAware?beans and invoke the?setServletContext?method accordingly.
轉載于:https://www.cnblogs.com/jiligalaer/p/4443481.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的Spring中DispacherServlet、WebApplicationContext、ServletContext的关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何编写python代码
- 下一篇: mysql触发器trigger实例详解