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

And*_*ili 21 java spring servlets spring-mvc java-ee

我正在学习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/");
   }
}
Run Code Online (Sandbox Code Playgroud)

我很清楚DispatcherServlet是如何工作的.我的怀疑与背景概念有关.

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

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

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

TNX

M. *_*num 30

根上下文

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

ContextLoaderListener寄存器此背景下的ServletContext名下org.springframework.web.context.WebApplicationContext.ROOT.

如果您ApplicationContext自己加载并使用上面的名称注册它,ServletContext那么将有资格作为根上下文.

儿童语境

Spring应用程序中的子上下文ApplicationContext是由DispatcherServlet(或者例如MessageDispatcherServlet在Spring-WS应用程序中)加载的.对于Spring MVC,此上下文应仅包含与该上下文相关的bean,即ViewResolvers,HandlerMappings等.

servlet ServletContext在名称下注册此上下文org.springframework.web.servlet.FrameworkServlet.CONTEXT.<servlet-name>.

Root <-Child Relation

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

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