I have a Spring web app with an applicationContext.xml and a dispatcher-servlet.xml configuration. I\'ve defined the
in applic
In our applications we define in the dispatcher-servlet.xml
I believe that is where it is supposed to be rather than in the applicationContext.xml
This section of the Spring documentation should provide more information:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
As you can see from one of the diagrams in section 16.2 the dispatcher-servlet sits above the applicationContext in the context hierarchy.
You are right - there are two different application contexts, the root application context loaded up by ContextLoaderListener (at the point the ServletContext gets initialized), and the Web Context (loaded up by DispatcherServlet), the root application context is the parent of the Web context.
Now, since these are two different application contexts, they get acted on differently - if you define component-scan
for your services in application context, then all the beans for the services get created here.
When your Dispatcher servlet loads up it will start creating the Web Context, at some point(driven by <mvc:annotation-driven/>
it will create a mapping for your uri's to handler methods, it will get the list of beans in the application context(which will be the web application context, not the Root application Context) and since you have not defined a component-scan
here the controller related beans will not be found and the mappings will not get created, that is the reason why you have to define a component-scan in the dispatcher servlets context also.
A good practice is to exclude the Controller related beans in the Root Application Context:
<context:component-scan base-package="package">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
and only controller related one's in Web Application Context:
<context:component-scan base-package="package" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
I had the same problem and after comparing my web.xml
code with this tutorial I changed it and it worked. here is my web.xml
file:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
context-param
and listener
were what I missed.
I hope it helps you.