【问题标题】:Spring MVC combined XML and annotation Controller configurationSpring MVC 结合 XML 和注解 Controller 配置
【发布时间】:2017-01-23 10:43:34
【问题描述】:

我正在将一个大型 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. 启动时中止

以这种方式配置的方法有很多。其中一些在我无法更改的基库类中具有@RequestMapping。

我可以通过将 url 路径从 XML 配置移动到注释来解决它,但我想避免这种情况(出于各种原因)并复制 Spring 3.0 的行为。

这可能吗?搜索答案未成功。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    更新:

    TL;DR:从 Spring 3.1 开始,这是不可能的

    阅读 SO“相关问题”链接: SpringMVC 3.0 to 3.1 migration of ControllerClassNameHandlerMapping

    引导我: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

    其中包含摘录:

    Spring MVC 3.1 中 @RequestMapping 方法的新支持类

    ...

    还有几件事不再可能:

    首先使用 SimpleUrlHandlerMapping 或 BeanNameUrlHandlerMapping 再根据缩小方法 @RequestMapping 注解。

    这解释了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2012-09-20
      • 2016-10-13
      • 2014-07-11
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多