根上下文和调度程序servlet上下文到底是如何进入Spring MVC Web应用程序的? [英] How exactly are the root context and the dispatcher servlet context into a Spring MVC web application?
问题描述
我正在学习 Spring MVC ,我有一些疑问相关
I am studying Spring MVC and I have some doubt related
所以,我有这个配置类来配置我的 DispatcherServlet 处理用户请求:
So, I have this configuration class that configure my DispatcherServlet that handle the user requests:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = ...
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("main", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("main/");
}
}
我很清楚如何 DispatcherServlet 有效。我的怀疑与上下文概念有关。
It is pretty clear for me how the DispatcherServlet works. My doubts are related to the context concept.
1)究竟代表上下文的是什么?我认为这就像是一组具有特定目标并且可以在环境中工作的bean。但我对这个断言绝对不是真的。
1) What exactly represent a context? I think that is something like a set of beans that have a specific pourpose and that works togheter into an environment. But I am absolutly not true about this assertion.
2)根上下文和调度程序servlet上下文之间有什么区别?
3)根据我的理解, dispatcherContext 中定义的bean可以访问 rootContext <中定义的bean / strong>(但相反的情况并非如此)。为什么?
3) From what I have understand the beans defined in dispatcherContext have access to beans defined in rootContext (but the opposite is not true). Why?
Tnx
推荐答案
Root Context
Spring应用程序中的根上下文是由 ContextLoaderListener $ c加载的
ApplicationContext
$ C>。此上下文应具有全局可用资源,如服务,存储库,基础结构bean( DataSource
, EntityManagerFactory
等)等。
Root Context
The root-context in a Spring application is the ApplicationContext
that is loaded by the ContextLoaderListener
. This context should have globally available resources like services, repositories, infrastructure beans (DataSource
, EntityManagerFactory
s etc.) etc.
ContextLoaderListener
在 ServletContext
下注册此上下文名称 org.springframework.web.context.WebApplicationContext.ROOT
。
The ContextLoaderListener
registers this context in the ServletContext
under the name org.springframework.web.context.WebApplicationContext.ROOT
.
如果您自己加载 ApplicationContext
并在<$ c $中使用上面的名称注册它c> ServletContext 然后将作为根上下文。
If you load an ApplicationContext
yourself and register it with the name above in the ServletContext
that will then qualify as the root-context.
Spring应用程序中的子上下文是由 DispatcherServlet
加载的 ApplicationContext
(或者例如Spring-WS应用程序中的 MessageDispatcherServlet
。这个上下文应该只包含与该上下文相关的bean,对于Spring MVC,它将是 ViewResolver
s, HandlerMapping
s等。
The child-context in a Spring application is the ApplicationContext
that is loaded by a DispatcherServlet
(or for instance a MessageDispatcherServlet
in a Spring-WS application). This context should only contain beans relevant to that context, for Spring MVC that would be ViewResolver
s, HandlerMapping
s etc.
servlet在名称 org.springframework.web下的
。 ServletContext
中注册此上下文。.servlet.FrameworkServlet.CONTEXT< servlet的名称>
The servlet registers this context in the ServletContext
under the name org.springframework.web.servlet.FrameworkServlet.CONTEXT.<servlet-name>
.
只有子上下文可以访问父上下文,因为你可能有多个孩子的情境。例如,在Spring MVC中结合Spring WS应用程序。子节点通过使用众所周知的名称在 ServletContext
中找到它来检测父上下文。
Only child contexts have access to the parent context, because you could have multiple child contexts. For instance in an Spring MVC combined with Spring WS application. The parent-context is detect by the childs by finding it in the ServletContext
with the well known name.
如果根上下文可以访问孩子哪个用于连接bean?接下来如果是这样的话,当涉及到AOP时你也会得到令人惊讶的结果。子上下文中定义的AOP会突然影响根上下文中配置的bean。
If the root context would have access to the child which one would it use to wire beans? Next to that if that would be the case you would also get surprising results when AOP is involved. AOP defined in the child context would suddenly influence beans configured in the root context.
这篇关于根上下文和调度程序servlet上下文到底是如何进入Spring MVC Web应用程序的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!