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
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.
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.).
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).
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
, EntityManagerFactory
s 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.
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 ViewResolver
s, HandlerMapping
s etc.
The servlet registers this context in the ServletContext
under the name org.springframework.web.servlet.FrameworkServlet.CONTEXT.<servlet-name>
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With