日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中DispacherServlet、WebApplicationContext、ServletContext的关系

發布時間:2023/12/20 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中DispacherServlet、WebApplicationContext、ServletContext的关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

解釋一:

? ? 要想很好理解這三個上下文的關系,需要先熟悉spring是怎樣在web容器中啟動起來的。spring的啟動過程其實就是其IoC容器的啟動過程,對于web程序,IoC容器啟動過程即是建立上下文的過程。

spring的啟動過程:

  • 首先,對于一個web應用,其部署在web容器中,web容器提供其一個全局的上下文環境,這個上下文就是ServletContext,其為后面的spring IoC容器提供宿主環境;

  • 其次,在web.xml中會提供有contextLoaderListener。在web容器啟動時,會觸發容器初始化事件,此時contextLoaderListener會監聽到這個事件,其contextInitialized方法會被調用,在這個方法中,spring會初始化一個啟動上下文,這個上下文被稱為根上下文,即WebApplicationContext,這是一個接口類,確切的說,其實際的實現類是XmlWebApplicationContext。這個就是spring的IoC容器,其對應的Bean定義的配置由web.xml中的context-param標簽指定。在這個IoC容器初始化完畢后,spring以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE為屬性Key,將其存儲到ServletContext中,便于獲取;

  • 再次,contextLoaderListener監聽器初始化完畢后,開始初始化web.xml中配置的Servlet,這個servlet可以配置多個,以最常見的DispatcherServlet為例,這個servlet實際上是一個標準的前端控制器,用以轉發、匹配、處理每個servlet請求。DispatcherServlet上下文在初始化的時候會建立自己的IoC上下文,用以持有spring mvc相關的bean。在建立DispatcherServlet自己的IoC上下文時,會利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先從ServletContext中獲取之前的根上下文(即WebApplicationContext)作為自己上下文的parent上下文。有了這個parent上下文之后,再初始化自己持有的上下文。這個DispatcherServlet初始化自己上下文的工作在其initStrategies方法中可以看到,大概的工作就是初始化處理器映射、視圖解析等。這個servlet自己持有的上下文默認實現類也是mlWebApplicationContext。初始化完畢后,spring以與servlet的名字相關(此處不是簡單的以servlet名為Key,而是通過一些轉換,具體可自行查看源碼)的屬性為屬性Key,也將其存到ServletContext中,以便后續使用。這樣每個servlet就持有自己的上下文,即擁有自己獨立的bean空間,同時各個servlet共享相同的bean,即根上下文(第2步中初始化的上下文)定義的那些bean。

  • 解釋二:

    在Web容器(比如Tomcat)中配置Spring時,你可能已經司空見慣于web.xml文件中的以下配置代碼: [plain]?view plaincopy
  • <span?style="font-family:SimSun;font-size:14px;"><context-param>??
  • ????????<param-name>contextConfigLocation</param-name>??
  • ????????<param-value>/WEB-INF/applicationContext.xml</param-value>??
  • ????</context-param>??
  • ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  • ????<listener>??
  • ????????<listener-class>??
  • ????????????org.springframework.web.context.ContextLoaderListener??
  • ????????</listener-class>??
  • ????</listener>??
  • ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  • ????<servlet>??
  • ????????<servlet-name>mvc-dispatcher</servlet-name>??
  • ????????<servlet-class>??
  • ????????????org.springframework.web.servlet.DispatcherServlet??
  • ????????</servlet-class>??
  • ????????<load-on-startup>1</load-on-startup>??
  • ????</servlet>??
  • ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  • ????<servlet-mapping>??
  • ????????<servlet-name>mvc-dispatcher</servlet-name>??
  • ????????<url-pattern>/</url-pattern>??
  • ????</servlet-mapping></span>??
  • ? ? 以上配置首先會在ContextLoaderListener中通過<context-param>中的applicationContext.xml創建一個ApplicationContext,再將這個ApplicationContext塞到ServletContext里面,通過ServletContext的setAttribute方法達到此目的,在ContextLoaderListener的源代碼中,我們可以看到這樣的代碼:

    [java] view plaincopy
  • <span?style="font-family:SimSun;font-size:14px;">servletContext.setAttribute(??
  • ??????????????WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,??
  • ?this.context);</span>??
  • ? ? 以上由ContextLoaderListener創建的ApplicationContext是共享于整個Web應用程序的,而你可能早已經知道,DispatcherServlet會維持一個自己的ApplicationContext,默認會讀取/WEB-INFO/<dispatcherServletName>-servlet.xml文件,而我么也可以重新配置:

    [plain] view plaincopy
  • <span?style="font-family:SimSun;font-size:14px;"><servlet>??
  • ????<servlet-name>??
  • ???????customConfiguredDispacherServlet??
  • ???</servlet-name>??
  • ???<servlet-class>??
  • ???????org.springframework.web.servlet.DispatcherServlet??
  • ???</servlet-class>??
  • ???<init-param>??
  • ???????<param-name>??
  • ???????????contextConfigLocation??
  • ???????</param-name>??
  • ???????<param-value>??
  • ???????????/WEB-INF/dispacherServletContext.xml??
  • ???????</param-value>??
  • ???</init-param>??
  • ???<load-on-startup>1</load-on-startup>??
  • </servlet></span>??
  • ? ? 問題是:以上兩個ApplicationContext的關系是什么,它們的作用作用范圍分別是什么,它們的用途分別是什么?

    ? ? ContextLoaderListener中創建ApplicationContext主要用于整個Web應用程序需要共享的一些組件,比如DAO,數據庫的ConnectionFactory等。而由DispatcherServlet創建的ApplicationContext主要用于和該Servlet相關的一些組件,比如Controller、ViewResovler等。

    ? ? 對于作用范圍而言,在DispatcherServlet中可以引用由ContextLoaderListener所創建的ApplicationContext,而反過來不行。

    ? ? 在Spring的具體實現上,這兩個ApplicationContext都是通過ServletContext的setAttribute方法放到ServletContext中的。但是,ContextLoaderListener會先于DispatcherServlet創建ApplicationContext,DispatcherServlet在創建ApplicationContext時會先找到由ContextLoaderListener所創建的ApplicationContext,再將后者的ApplicationContext作為參數傳給DispatcherServlet的ApplicationContext的setParent()方法,在Spring源代碼中,你可以在FrameServlet.java中找到如下代碼:

    wac.setParent(parent);

    ? ? 其中,wac即為由DisptcherServlet創建的ApplicationContext,而parent則為有ContextLoaderListener創建的ApplicationContext。此后,框架又會調用ServletContext的setAttribute()方法將wac加入到ServletContext中。

    當Spring在執行ApplicationContext的getBean時,如果在自己context中找不到對應的bean,則會在父ApplicationContext中去找。這也解釋了為什么我們可以在DispatcherServlet中獲取到由ContextLoaderListener對應的ApplicationContext中的bean。

    Spring API中的解釋:

    public interface WebApplicationContextextends?ApplicationContext

    Interface 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

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的Spring中DispacherServlet、WebApplicationContext、ServletContext的关系的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。