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

查看:25
本文介绍了为什么 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?

谢谢!

推荐答案

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

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天全站免登陆