简体   繁体   中英

Why DispatcherServlet creates another application context?

I have configured the root application context using ContextLoaderListener and the context init-parameter contextConfigLocation .

The root context is then accessed by JSF (*.jsf) variable-resolver. It works fine.

Now the problem is, the requests (*.do) going thru DispatcherServlet will get another application context, and singleton beans are then instantiated twice.

I don't need another application context for DispatcherServlet , how can I specify it to re-use the existing root application context, which is loaded by ContextLoaderListener ?

NOTE

After read the reference pages in answers, I know there is a context separation between the root context and the dispatcher context, but none of the references tell me where to go. So here is my solution, maybe helpful for other people facing the similar question:

  1. In the context config XML for the dispatcher servlet: dispatcher-servlet.xml , I have duplicated defined <context:component-scan/> which is already defined in the root context. So remove it. The dispatcher-servlet.xml only have to define those beans used for Spring MVC only.

  2. All the controllers have already been scanned and instantiated in the root context, however, Spring MVC by default doesn't register the controllers in the root context for request mappings. You can either:

    2.1. In the root context, exclude @Controller from <component-scan> , and scan @Controller only in the dispatcher-servlet.xml.

    2.2. Or, set the property DefaultAnnotationHandlerMapping.detectHandlersInAncestorContexts to true:

     (dispatcher-servlet.xml:) <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="detectHandlersInAncestorContexts" value="true" /> </bean> 

To answer your first question, the DispatcherServlet creates a context because that's how it allows itself to be configured, and if you have multiple DispatcherServlets in one app, they'd each need to be configured separately. Therefore each one gets its own context, and each of those contexts is separate from the "root" context, where all of your real, "working" beans should live so they can be shared between the other contexts. There have been a number of questions over the last couple of weeks that were spawned by confusion over this very issue. You might gain a better understanding of how things work by checking out the answers:

Spring XML file configuration hierarchy help/explanation

Declaring Spring Bean in Parent Context vs Child Context

Spring-MVC: What are a "context" and "namespace"?

If you have a DispatcherServlet running, there is no need to use ContextLoaderListener . Just use ContextLoader.getCurrentWebApplicationContext() to access the WebApplicationContext .

Just keep the bean definitions separate as outlined in this previous answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM