ContextLoaderListener與DispatcherServlet所載入的applicationContext的區別

來源:互聯網
上載者:User

標籤:io   ar   使用   sp   div   on   art   bs   ad   

第一種配置方式:<servlet>      <servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath*:applicationContext*.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>*.do</url-pattern>  </servlet-mapping> 第二種配置方式:<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml,classpath:*applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> 區別:ContextLoaderListener中載入的context成功後,spring 將 applicationContext存放在ServletContext中key值為"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。DispatcherServlet載入的context成功後,如果 publishContext屬性的值設定為true的話(預設為true) 會將applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。結論:從上面的分析可以看出,DispatcherServlet所載入的applicationContext可以認為是mvc私人的context,由於儲存在servletContext中的key值與通過ContextLoaderListener載入進來的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet載入context的話,如果程式中有地方使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 來試圖擷取applicati

ContextLoaderListener與DispatcherServlet所載入的applicationContext的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.