根上下文和调度程序servlet上下文到底是如何进入Spring MVC Web应用程序的?

我正在学习Spring MVC ,我有一些疑问

所以,我有这个配置类来配置处理用户请求的DispatcherServlet

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是如何工作的。 我的怀疑与背景概念有关。

1)究竟代表什么背景 ? 我认为这就像是一组具有特定目标并且可以在环境中工作的bean。 但我绝对不是这个断言的真实。

2) 根上下文调度程序servlet上下文之间有什么区别?

3)根据我的理解, dispatcherContext中定义的bean可以访问rootContext中定义的bean (但相反的情况并非如此)。 为什么?

TNX

根上下文

Spring应用程序中的根上下文是由ContextLoaderListener加载的ApplicationContext 。 此上下文应具有全局可用资源,如服务,存储库,基础结构bean( DataSourceEntityManagerFactory等)等。

ContextLoaderListenerServletContext中以org.springframework.web.context.WebApplicationContext.ROOT的名称注册此上下文。

如果您自己加载ApplicationContext并在ServletContext中使用上面的名称注册它,那么它将符合作为根上下文的条件。

儿童语境

Spring应用程序中的子上下文是由DispatcherServlet (或Spring-WS应用程序中的MessageDispatcherServlet )加载的ApplicationContext 。 对于Spring MVC,这个上下文应该只包含与该上下文相关的bean,它们是ViewResolverHandlerMapping等。

servlet在ServletContext中以org.springframework.web.servlet.FrameworkServlet.CONTEXT.的名称注册此上下文。

Root <-Child Relation

只有子上下文才能访问父上下文,因为您可以拥有多个子上下文。 例如,在Spring MVC中结合Spring WS应用程序。 子节点通过在ServletContext使用众所周知的名称查找它来检测父上下文。

如果根上下文可以访问孩子哪个用于连接bean? 接下来如果是这样的话,当涉及到AOP时你也会得到令人惊讶的结果。 子上下文中定义的AOP会突然影响根上下文中配置的bean。