Java 为什么 Spring MVC 至少需要两个上下文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18682486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:08:30  来源:igfitidea点击:

Why does Spring MVC need at least two contexts?

javaspringservletsspring-mvc

提问by liam xu

In Spring MVC, there are two contexts. One is the application context or global context which is booted up by ContextLoaderListener. It takes all the configuration files mentioned in contextConfigLocationparameter.

在 Spring MVC 中,有两种上下文。一种是由ContextLoaderListener. 它需要contextConfigLocation参数中提到的所有配置文件。

Now if you are using Spring MVC as well, then Dispatcher servlet is required, which boots up another container which is also known as web application container. This container takes the global container as a parent.

现在,如果您也使用 Spring MVC,则需要 Dispatcher servlet,它会启动另一个容器,也称为 Web 应用程序容器。该容器将全局容器作为父容器。

When integrating struts1 with spring, there is only one context. Why does spring mvc need two? Is it possible to use only one context when using spring mvc?

将 struts1 与 spring 集成时,只有一个上下文。为什么spring mvc需要两个?使用 spring mvc 时是否可以只使用一种上下文?

thanks!

谢谢!

采纳答案by Paul Grime

Imagine you had two separate Dispatchers, each serving a different purpose, and each having its own dependencies. You would configure those independently using separate contexts.

想象一下,您有两个独立的 Dispatcher,每个 Dispatcher 服务于不同的目的,并且每个都有自己的依赖项。您将使用单独的上下文独立配置这些。

If there is any shared configuration, this can go in the 'global' context.

如果有任何共享配置,这可以在“全局”上下文中进行。

I don't think it's possible to have only one context using DispatcherServlet, as it creates its own context and links it to the parent context (via the FrameworkServlet superclass).

我认为使用 DispatcherServlet 不可能只有一个上下文,因为它创建了自己的上下文并将其链接到父上下文(通过 FrameworkServlet 超类)。

FrameworkServlet.createWebApplicationContext

FrameworkServlet.createWebApplicationContext

回答by zagyi

Having a root web application context plus a child servlet context is just an option. If you know that your application won't have a second servlet, it's arguably simpler to have one single Spring context for the whole web application.

拥有一个根 Web 应用程序上下文和一个子 servlet 上下文只是一种选择。如果您知道您的应用程序不会有第二个 servlet,那么为整个 Web 应用程序拥有一个单一的 Spring 上下文可以说更简单。

You can achieve that setup by simply removing the ContextLoaderListener(and the accompanying contextConfigLocationcontext-param) from your web.xmland moving all bean definitions into the xml defining the servlet context ([servlet-name]-servlet.xml).

您可以通过简单地从您的ContextLoaderListener(以及随附的contextConfigLocation上下文参数)中删除(以及随附的上下文参数)web.xml并将所有 bean 定义移动到定义 servlet 上下文 ( [servlet-name]-servlet.xml)的 xml 中来实现该设置。

This is possible, because the FrameworkServlet(super-class of DispatcherServlet) doesn't care if there is a root application context when creating the servlet context. It just relays the root context as the parent if available. See related code here.

这是可能的,因为FrameworkServlet( 的超类DispatcherServlet)在创建 servlet 上下文时并不关心是否存在根应用程序上下文。如果可用,它只是将根上下文中继为父级。请参阅此处的相关代码

回答by beinghuman

Check this answer About multiple containers in spring framework

检查这个答案关于 spring 框架中的多个容器

Yes ,you can have one context only.

是的,您只能有一个上下文。

For code reuse it would be better to isolate services in Application Context rather then WebApplicationContext.but this not compulsion.you can keep only webApplicationcontext only.

对于代码重用,最好在应用程序上下文中隔离服务而不是 WebApplicationContext。但这不是强制性的。您只能保留 webApplicationContext。