tags:

views:

1297

answers:

2

Hi guys,

When I use DispatcherServlet, I get a java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? error when I use a DelegatingFilterProxy filter. Therefore I've removed the DispatcherServlet and now I use a ContextLoaderListener instead, and my Spring application loads fine. However, I have a problem with one VERY important bean:

   <context:component-scan base-package="com.mydomain"/>  
   <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
      <property name="interceptors">
         <list>
            <ref bean="openSessionInViewInterceptor" />
         </list>
      </property>
   </bean>

This bean no longer works, none of my @Controller's are URL mapped anymore. If I switch back to using DispatcherServlet, no problem (except that my filter is useless again). How can I get this bean to load correctly from within a ContextLoaderListener?

Cheers

Nik

+9  A: 

You need both the ContextLoaderListener and the DispatcherServlet - the error message didn't tell you to remove the servlet.

To clarify what Spring is doing here, the DispatcherServlet creates its own ApplicationContext (typically using xxx-servlet.xml), but any Spring Filters that you configure in web.xml don't have access to the servlet's ApplicationContext.

The ContextLoaderListener creates a second ApplicationContext (associated with the whole webapp), and links itself with the servlet's ApplicationContext, allowing filters and servlets to communicate via Spring.

skaffman
niklassaers
The appcontext created by ContextLoaderListener is the parent context of the servlet's appcontext. That means that the beans in the ContextLoaderListener's appcontext are automatically visible to the beans in the servlet's appcontext (but not the other way around). So any that are shared should be declared in the parent.
skaffman
Thank you very much, I was not aware of that. I fixed my config accordingly, and now it all works brilliantly. :-) Thank you very much, and thanks for all the other times you've helped me out here. I definitely need to buy you a couple of beers sometime :-)
niklassaers
+1 - skaffman, you're Da Man.
duffymo
aww shucks, y'all are making me blush
skaffman
This is not working, when I set two different context xmls, and set <context:component-scan base-package="org.my.app.base.package.path" /> in config xml that is set for ContextConfigListener, I only get white pages, but when that definition is in both files then it works.
newbie
A: 

The web.xml of the Spring MVC basic app doesn't have a ContextLoaderListener though? See https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/src/main/webapp/WEB-INF/web.xml.

Reason I ask is because I'm getting the following error in my application:

ERROR [[Spring MVC Dispatcher Servlet]] Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

and I don't have a ContextLoaderListener (which is likely to cause the issue).

Any thoughts as to why the MVC basic app will work?

Nes