jeudi 4 juin 2015

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

Aucun commentaire:

Enregistrer un commentaire