为什么Spring MVC需要至少两个上下文? [英] Why does Spring MVC need at least two contexts?

查看:241
本文介绍了为什么Spring MVC需要至少两个上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring MVC中,有两种情况。一个是由 ContextLoaderListener 启动的应用程序上下文或全局上下文。它需要 contextConfigLocation 参数中提到的所有配置文件。

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 contextConfigLocation parameter.

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

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.

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

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?

谢谢!

推荐答案

想象一下,你有两个独立的Dispatchers,每个Dispatchers都有不同的用途,每个都有自己的依赖关系。您可以使用单独的上下文独立配置它们。

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.

如果有任何共享配置,则可以进入全局上下文。

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

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

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).

FrameworkServlet.createWebApplicationContext

这篇关于为什么Spring MVC需要至少两个上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆