我正在使用 Spring 3.0.5 和 <context:component-scan>
和 @RequestMapping
我的 Controller 上的注释。这有效,并且 URL 由包扫描注册。
但是当我在 XML 配置中定义了处理程序映射时,就会出现问题。 @RequestMapping
不再提取注释。
我已将问题隔离到一个简单的应用程序中。
如果我有以下 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";
}
}
和以下配置:
<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>
应用程序运行正常,URL /test
已注册并正常工作。
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'
但是,如果我将处理程序映射添加到 XML,它就不再有效了。即使是这样简单的事情:
<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />
基本上什么都不做,<context:component-scan>
不再注册我的网址。
我需要为一些我无法注释的(第三方) Controller 提供额外的处理程序映射,但是在添加它时它会破坏我所有的 @RequestMapping
这正常吗?错误? (我无法更改 Spring 版本)
我错过了什么吗?
最佳答案
Is this normal? A bug? (I can't change the Spring version)
Am I missing something?
我遗漏了一些东西 :D。在 DefaultAnnotationHandlerMapping 的 Spring JavaDocs 中发现了这个:
NOTE: If you define custom HandlerMapping beans in your DispatcherServlet context, you need to add a DefaultAnnotationHandlerMapping bean explicitly, since custom HandlerMapping beans replace the default mapping strategies.
将其添加到我的配置 XML 中,现在一切正常:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
关于java - <上下文 :component-scan> not picking up my @RequestMappings if handler mappings defined in XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7463130/