I am studying Spring MVC and I have some doubt related
So, I have this configuration class that configure my DispatcherServlet that
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.