<context: component-scan> does not collect my @RequestMappings if handler mappings are defined in XML
I use Spring 3.0.5 with annotations <context:component-scan>and @RequestMappingon my controllers. This works and URLs are registered by packet scanning.
But there is a problem when I have a handler mapping defined in the XML configuration. Annotations are @RequestMappingno longer matched.
I highlighted the problem for a simple application.
If I have the following controller:
package test.pack.age;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/test")
public String showTestPage() {
return "testPage";
}
}
and the following configuration:
<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The application works correctly and the URL is /testregistered and works correctly.
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
But if I add a handler mapping in XML, it no longer works. Even something simple:
<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />
which basically does nothing but <context:component-scan>no longer registers my URL.
() , , @RequestMapping s.
? ? ( Spring)
- ?
? ? ( Spring)
- ?
- : D. Spring JavaDocs DefaultAnnotationHandlerMapping:
. HandlerMapping beans DispatcherServlet, DefaultAnnotationHandlerMapping bean, HandlerMapping beans .
XML, :
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />