簡體   English   中英

Web應用程序中的ContextLoaderListener和Servlet上下文是什么?

[英]what is ContextLoaderListener and Servlet context in a web application?

任何人都可以參考下面提到的從spring-reference引用的語句來解釋這一點。

如果您不想使用Spring的Web MVC,但打算利用Spring提供的其他解決方案,則可以輕松地將自己選擇的Web MVC框架與Spring集成在一起。 只需通過其ContextLoaderListener啟動Spring根應用程序上下文,然后從Struts或WebWork動作中通過其ServletContext屬性(或Spring的相應輔助方法)對其進行訪問。 不涉及“插件”,因此不需要專門的集成。

這意味着,如果您不想使用Spring MVC,請使用ContextLoaderListener在ServletContext中引導WebApplicationContext。 像這樣:

<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

然后使用WebApplicationContextUtils方法getRequiredWebApplicationContext(ServletContext sc) (或其他多種方法)來獲取WebApplicationContext並訪問您的Spring Bean。

您可以在應用程序的任何部分中執行此操作(只要可以獲得ServletContext)。 這意味着您不必將Spring集成到其他技術中。

Servlet上下文是Web應用程序目錄的根。 其他所有內容都相對於它給出了參考。 上下文加載器偵聽器是加載Spring上下文的第二種方法,首先是使用分派器servlet。

應用程序上下文是由web.xml中定義的ContextLoaderListenerContextLoaderServlet (dispatcher-servlet)初始化的容器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM