Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 384
AndreaNobili Avatar asked Jun 04 '15 09:06

AndreaNobili


People also ask

What is the root application context in Spring MVC?

ApplicationContext (Root Application Context) : Every Spring MVC web application has an applicationContext. xml file which is configured as the root of context configuration. Spring loads this file and creates an applicationContext for the entire application.

What is root context and child context in Spring MVC?

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.).

Does root context belong to dispatcher servlet?

ContextLoaderListener creates root application context. DispatcherServlet entries create one child application context per servlet entry. Child contexts can access beans defined in root context. Beans in root context cannot access beans in child contexts (directly).


1 Answers

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.

like image 105
M. Deinum Avatar answered Oct 18 '22 01:10

M. Deinum