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

前端 未结 1 1806
星月不相逢
星月不相逢 2020-12-23 12:24

I am studying Spring MVC and I have some doubt related

So, I have this configuration class that configure my DispatcherServlet that

相关标签:
1条回答
  • 2020-12-23 12:54

    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.

    0 讨论(0)
提交回复
热议问题