31

I am studying Spring MVC and I have some doubt related

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/");
   }
}

It is pretty clear for me how the DispatcherServlet works. My doubts are related to the context concept.

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) What is the difference between the root context and the dispatcher servlet context?

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

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Your WAR file is the context. – duffymo Jun 04 '15 at 09:22
  • 2
    Beans/configuraiton loaded by the `ContextLoaderListener` is the root context, everything loaded by a `DispatcherServlet` (or `MessageDispatcherServlet` for Spring-WS) is a child context. You can have multiple servlets which all have access to the root context (should contain shared resources like services, etc.). Now if you have 10 dispatcher servlets, which servlet should be accessed by the root context? Hence only the parent is accessible from the child and not the other way around. This would also lead to issues with AOP, suddenly AOP defined by the child would influence the parent. – M. Deinum Jun 04 '15 at 09:27
  • @M.Deinum Tnx so much, perfect explaination. If you put it as a response I will accept it :-) – AndreaNobili Jun 04 '15 at 09:31
  • http://stackoverflow.com/questions/19619539/understanding-contexts-in-spring-mvc – Shailendra Jun 04 '15 at 09:44
  • [link1](http://www.codesenior.com/en/tutorial/Spring-ContextLoaderListener-And-DispatcherServlet-Concepts) , [link2](http://www.deroneriksson.com/tutorials/java/spring/introduction-to-the-spring-framework/configuring-root-web-application-context-in-spring-web-application) have good explanations. –  Feb 16 '17 at 12:21

1 Answers1

42

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, EntityManagerFactorys etc.) etc.

The ContextLoaderListener registers this context in the ServletContext under the name org.springframework.web.context.WebApplicationContext.ROOT.

If you load an ApplicationContext yourself and register it with the name above in the ServletContext that will then qualify as the root-context.

Child Context

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 ViewResolvers, HandlerMappings etc.

The servlet registers this context in the ServletContext under the 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 ServletContext with the well known name.

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.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
M. Deinum
  • 115,695
  • 22
  • 220
  • 224