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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30639842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:25:40  来源:igfitidea点击:

How exactly are the root context and the dispatcher servlet context into a Spring MVC web application?

javaspringspring-mvcjakarta-eeservlets

提问by AndreaNobili

I am studying Spring MVCand I have some doubt related

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

So, I have this configuration class that configure my DispatcherServletthat handle the user requests:

所以,我有这个配置类来配置我的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/");
   }
}

It is pretty clear for me how the DispatcherServletworks. My doubts are related to the contextconcept.

我很清楚DispatcherServlet是如何工作的。我的怀疑与上下文概念有关。

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.

1) 究竟什么代表上下文?我认为这就像一组具有特定倾倒姿势并且可以一起工作到环境中的 bean。但我对这个断言绝对不是真的。

2) What is the difference between the root contextand the dispatcher servlet context?

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

3) From what I have understand the beans defined in dispatcherContexthave access to beans defined in rootContext(but the opposite is not true). Why?

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

Tnx

田纳西州

回答by M. Deinum

Root Context

根上下文

The root-context in a Spring application is the ApplicationContextthat is loaded by the ContextLoaderListener. This context should have globally available resources like services, repositories, infrastructure beans (DataSource, EntityManagerFactorys etc.) etc.

Spring 应用程序中的根上下文ApplicationContext是由ContextLoaderListener. 这个上下文应该有全球可用的资源,比如服务、存储库、基础设施 bean(DataSourceEntityManagerFactorys 等)等。

The ContextLoaderListenerregisters this context in the ServletContextunder the name org.springframework.web.context.WebApplicationContext.ROOT.

ContextLoaderListener注册此背景下的ServletContext名下org.springframework.web.context.WebApplicationContext.ROOT

If you load an ApplicationContextyourself and register it with the name above in the ServletContextthat will then qualify as the root-context.

如果您ApplicationContext自己加载并使用上面的名称注册它,ServletContext那么它将被视为根上下文。

Child Context

子上下文

The child-context in a Spring application is the ApplicationContextthat is loaded by a DispatcherServlet(or for instance a MessageDispatcherServletin a Spring-WS application). This context should only contain beans relevant to that context, for Spring MVC that would be ViewResolvers, HandlerMappings etc.

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

The servlet registers this context in the ServletContextunder the name org.springframework.web.servlet.FrameworkServlet.CONTEXT.<servlet-name>.

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

Root <-Child Relation

根<-子关系

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 children by finding it in the ServletContextwith the well known name.

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

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.

如果根上下文可以访问子级,它将使用哪一个来连接 bean?除此之外,如果是这种情况,当涉及 AOP 时,您也会得到令人惊讶的结果。在子上下文中定义的 AOP 会突然影响在根上下文中配置的 bean。