![](/img/trans.png)
[英]To remove off Spring @controller annotation and use xml configuration
[英]Spring MVC combined XML and annotation Controller configuration
我正在將一個大型Spring 3.0應用程序移植到Spring 3.2(是的,我知道)。 該應用程序結合了XML和注釋配置來定義路由,例如:
servlet.xml:
<context:annotation-config/>
<context:component-scan base-package="foo.bar" />
...
<mvc:annotation-driven />
...
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
...
<property name="mappings">
<props>
<prop key="/booking/default.htm">booking.default</prop>
...
</props>
</property>
...
</bean>
<bean id="booking.default" class="foo.bar.BookingController">
...
</bean>
BookingController.java
@Controller
public class BookingController {
...
@RequestMapping(method = RequestMethod.GET)
public String handleRequest(...)
...
}
在Spring 3.0中,效果是將GET /booking/default.htm映射到BookingController的handleRequest方法,但是我無法在Spring 3.2中重新創建此行為。
看來,Spring 3.2會忽略XML,並將使用@RequestMapping(method = RequestMethod.GET)
注釋的每個方法都視為相同,並且在啟動時會因java.lang.IllegalStateException: Ambiguous mapping found.
中止java.lang.IllegalStateException: Ambiguous mapping found.
通過這種方式配置了許多方法。 其中一些在我無法更改的基礎庫類中具有@RequestMapping。
我可以通過將URL路徑從XML配置移動到注釋來解決此問題,但是我想避免這種情況(由於各種原因)並復制Spring 3.0行為。
那可能嗎? 搜索答案未成功。
更新:
TL; DR:從Spring 3.1開始,這是不可能的
閱讀SO的“相關問題”鏈接: SpringMVC 3.0到ControllerClassNameHandlerMapping的3.1移植
帶我去: http : //docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
其中包含摘錄:
Spring MVC 3.1中@RequestMapping方法的新支持類
...
還有幾件事不再可能:
首先使用SimpleUrlHandlerMapping或BeanNameUrlHandlerMapping選擇一個控制器,然后根據@RequestMapping注釋縮小方法范圍。
這解釋了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.