春天:为什么“根”应用程序上下文和“servlet”应用程序上下文是由不同方创建的? [英] Spring: Why "root" application context and "servlet" application context are created by different parties?
问题描述
据我了解,基于Spring的Web应用程序初始化如下:
As I understand, a Spring based web application is initialized as below:
步骤1 : Servlet容器(例如Tomcat)
找到 ServletContainerInitializer
的实现,它是 SpringServletContainerInitializer
。
Step 1: Servlet container (e.g. Tomcat)
locates the implementation of ServletContainerInitializer
, which is SpringServletContainerInitializer
.
第2步: SpringServletContainerInitializer
创建 DispatcherServlet
和 ContextLoaderListener
第3步: DispatcherServlet
创建 servlet应用程序上下文
。并且 ContextLoaderListener
创建根应用程序上下文
。
步骤1由Servlet 3.0规范定义。第2,3步完全由Spring定义。
Step 1 is defined by Servlet 3.0 spec. Step 2, 3 are totally defined by Spring.
我可以看到在 web bean的理由根上下文中的> servlet上下文和非Web
bean。但是为什么我们必须在不同的位置创建这两个上下文,即 DispatcherServlet
和 ContextLoaderListener
?
I can see the rational of putting web
beans in servlet context and non-web
beans in root context. But why do we have to create these 2 contexts in different places, i.e. DispatcherServlet
and ContextLoaderListener
?
如果所有我们想要 只是 来准备一切必要的,为什么不只是在 ContextLoaderListener
中创建两个上下文,因为它可以被视为整个Web应用程序的 main()
方法。我认为这是更多的逻辑和当前的方法只会使事情变得复杂。
If all we want is just to prepare everything necessary, why not just create both contexts in ContextLoaderListener
since it can be seen as the main()
method of the whole web application. I think that's more logic and current approach only complicates things up.
基于@ Shailendra的回复,我画这个:
Based on @Shailendra's reply, I draw this:
我的理解是, Spring引入了应用程序上下文
概念,并将它们存储在 Servlet Context
中。 Servlet Context是java servlet technolgoy引入的一个概念。
My understanding is, Spring introduced the application context
concepts and store them in the Servlet Context
. Servlet Context is a concept introduced by java servlet technolgoy.
我想 DispatcherServlet
实现应该有一个成员变量to将键
保存到 servlet上下文
servlet应用程序上下文 >。所以它可以访问它自己的上下文。也许关键是servlet名称。
I guess the DispatcherServlet
implementation should have a member variable to hold the key
to its servlet application context
in the servlet context
. So it can access it's own context. Maybe the key is the servlet name.
根应用程序上下文
应该有知名键,以便每个人都可以访问它。
And the root application context
should have a well-known key so everyone can access it.
众所周知 根应用程序上下文
的键是这样的:
The well-known key for root application context
is this:
(在组织中)。 springframework.web.context.WebApplicationContext
)
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
ADD 3
DispatcherServlet
确实引用了它的 WebApplicationContext
。它从 FrameworkServlet
继承了以下memeber:
ADD 3
The DispatcherServlet
does have a reference to its WebApplicationContext
. It inherits the following memeber from FrameworkServlet
:
/** WebApplicationContext for this servlet */
private WebApplicationContext webApplicationContext;
和
public FrameworkServlet(WebApplicationContext webApplicationContext) {
this.webApplicationContext = webApplicationContext;
}
推荐答案
但是为什么我们必须在不同的地方创建这两个上下文,
即DispatcherServlet和ContextLoaderListener
But why do we have to create these 2 contexts in different places, i.e. DispatcherServlet and ContextLoaderListener
因为这两个上下文应该是不同的,但仍然具有层次关系,以便能够覆盖。通常,使用 ContextLoaderListener
加载的上下文是属于整个应用程序的root上下文,而使用 DispatcherServlet
初始化的上下文是实际上特定于该servlet。从技术上讲,您可以在应用程序中拥有多个servlet,因此多个这样的上下文各自特定于相应的servlet但具有相同的根上下文。有关详细信息,请参阅我的另一个答案此处。
Because the two contexts are supposed to be different but yet have a hierarchical relation in order to be able to override. Typically the context loaded using ContextLoaderListener
is "root" context which belongs to the whole application while the one initialized using DispatcherServlet
is actually specific to that servlet. Technically you can have multiple servlets in an application and so multiple such contexts each specific for respective servlet but having same root context. For more details see another of my answer here.
这篇关于春天:为什么“根”应用程序上下文和“servlet”应用程序上下文是由不同方创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!