Spring-MVC:什么是 "context"和 "namespace"?

标签 spring spring-mvc

来自 XmlWebApplicationContext文档:

By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" for the root context, and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test").

Spring 上下文是什么意思?

什么是根上下文?还有哪些其他类型的 Spring 上下文?

什么是命名空间?

更新:

一些后续问题:

  1. 什么是 Spring ApplicationContext - 它是不是保存在配置 XML 文件中定义的 bean 的“东西”?

  2. 查看 ContextLoaderListener 的代码,它看起来像是加载了配置 XML 文件中定义的数据。但我的 Spring Web 应用程序无需定义此监听器或任何其他监听器即可工作。怎么可能?

  3. 在什么情况下拥有多个 Spring 的 DispatcherServlet 实例才有意义?

  4. 根上下文(applicationContext.xml 中的数据)是否适用于 DispatcherServlet 的每个实例,而其他上下文(例如 test-servlet.xml 中的数据)是否仅适用于相关的 DispatcherServlet(即 test)?

最佳答案

“Spring 上下文” = a Spring ApplicationContext .

“根上下文”,就 web 应用而言,是指 webapp 加载和使用的主要上下文。通常,您将使用 ContextLoaderListener 启动根上下文。 .

根上下文并不是真正的“种类”上下文。这只是上下文所扮演的角色。您在 webapp 中有一个根上下文。其他上下文不是根上下文。它们通常是根上下文的子级。

这里的命名空间是指 Spring 的 DispatcherServlet 实例的范围。它只是说,如果你在 web.xml 中将你的 servlet 命名为“test”,那么按照惯例,Spring 将寻找一个名为“test-servlet.xml”的文件来用作该调度程序的上下文。顺便说一句,像这样为调度程序创建的每个上下文都成为根上下文的子上下文。

编辑:回答您的新问题:

  1. 按照我的答案第一行中的链接了解 ApplicationContext。如果您有没有在那里回答的问题,我建议发布一个新的 SO 问题。
  2. 根上下文是可选的。如果您没有定义 ContextLoaderListener,那么您就没有根上下文。当您使用 DispatcherServlet 时,它会启动自己的 ApplicationContext,并从那里获取所需的 bean。
  3. 我不知道有哪一个在我的脑海中。我想如果您的应用中的某些 URL 资源之间需要完全不同的配置,这可能会促使您这样做。
  4. 是的。准确地说,根上下文是为 DispatcherServlet 启动的任何上下文的父上下文。父上下文中的 Bean 可以通过子上下文访问,但反之则不然。

关于Spring-MVC:什么是 "context"和 "namespace"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7451325/

相关文章:

spring - 如何将spring boot项目转换为spring mvc

java - createNativeQuery 中的分页

java - Spring Boot 2 Maven 目录 View 解析器

java - Spring Integration Bridge 注解配置导致 BeanCurrentlyInCreationException

java - Spring 管理属性的最佳方式

java - Spring MVC - 在 Controller 中默认设置值

java - 基于 Spring Profile 的 Jersey Rest 服务发布

java - 警告: Autowired annotation is not supported on static fields的原因是什么

java - 使用表单 :input 编写 Spring MVC 表单的正确方法是什么

java - 如何使用 Spring MVC Controller 作为集成测试的假端点?