繁体   English   中英

Spring MVC中的WebApplicationContext是什么? 它是干什么用的?

[英]What is the WebApplicationContext in Spring MVC? What is it used for?

我知道 Spring ApplicationContext 是什么,但我对来自 Spring MVC 的 WebApplicationContext 感到困惑。 你们能帮帮我吗?

  • 什么是 WebApplicationContext?
  • 为什么有必要?
  • 它是干什么用的?

您可以检查WebApplicationContext代码以了解情况:

public interface WebApplicationContext extends ApplicationContext {

   ServletContext getServletContext();
}

因为inheritance,你可以简单的认为WebApplicationContext只是一种特殊的ApplicationContext ,只用于web这个应用。 因此,所有ApplicationContext可以完成的事情也可以由WebApplicationContext完成。 但是WebApplicationContext只是提供了一个额外的 function ,它允许您访问由 Servlet API 规范定义的底层ServletContext

之所以有必要,主要是因为 OOP 的设计很好。 如果我们不把它分成两个接口,只是将getServletContext()添加到ApplicationContext中,并用于所有非web和web应用程序,那么对于非web应用程序,我们仍然需要包含Servlet是很尴尬的API JAR 到我们的项目,因为我们使ApplicationContext依赖于 Servlet API。

官方 Spring 文档包含了答案。

WebApplicationContext 是普通 ApplicationContext 的扩展,具有 web 应用程序所需的一些额外功能。 它与普通 ApplicationContext 的不同之处在于它能够解析主题(请参阅使用主题),并且它知道它与哪个 Servlet 关联(通过具有到 ServletContext 的链接)。 WebApplicationContext 绑定在 ServletContext 中,通过在 RequestContextUtils class 上使用 static 方法,如果需要访问它,您可以随时查找 WebApplicationContext。

它用于促进 web 服务器功能。 文档还提到了 WebApplicationContext 提供的特殊 bean 类型。 例如,“解析多部分请求以支持处理来自 HTML forms 的文件上传”的 MultiPartResolver。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM