<mvc:annotation-driven />做什么?

Kis*_*Kim 12 spring-mvc

我在我的REST API中使用文件名(例如:GET http://xxx/api/myImage.jpg)问题是@PathVariable丢弃".jpg".这个问题已经在这里问过几次并回答了.但对我不起作用.

所以我搜索了然后发现了

https://jira.springsource.org/browse/SPR-6524

"......根本不应该与手动DefaultAnnotationHandlerMapping实例结合使用;这被设计为目前的一种或两种选择,非常类似于和."

"mvc名称空间是简化配置".

真正的问题是mvc做了什么?并改变了?

我发现自己这些东西..

  1. intercepter配置已更改.(bean配置中需要mvc namspace)
  2. useDefaultSuffixPattern不起作用.
  3. 添加JSON消息转换器.如果杰克逊图书馆可用
  4. @PathVariable参数自动添加到模型中

还有其他人?

提前致谢!

Roy*_*ouh 12

mvc:annotationDriven标签基本上是将你的Spring上下文允许请求调度到控制器.

该标记将配置两个bean DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter.

您可以从spring文档中找到更多信息:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

  • 从Spring 3.1开始,配置的bean是RequestMappingHandlerMapping和RequestMappingHandlerAdapter (4认同)

Ani*_*kur 9

在我提供某些要点之前让我弄清楚罗伊提供的答案是不准确的.您不必提供mvc:annotation-driven标记来实例化默认bean.此标记可用于Spring 3.0+启用S​​pring 3.0中引入的新功能

(如果您想要向后兼容,请不要使用它,特别是如果您使用的是基于旧控制器的类,例如MultiActionController,SimpleFormController)

现在让我们来看看这个标签实际上做了什么 -

在Spring 3.1之前使用默认bean

  1. DefaultAnnotationHandlerMapping
  2. AnnotationMethodHandlerAdapter上
  3. AnnotationMethodHandlerExceptionResolver

这些在Spring 3.1中已弃用,如果您使用上述标记,它将被替换为 -

  1. RequestMappingHandlerMapping
  2. RequestMappingHandlerAdapter
  3. ExceptionHandlerExceptionResolver

DefaultAnnotationHandlerMapping决定使用哪个控制器,AnnotationMethodHandlerAdapter选择处理请求的实际方法.RequestMappingHandlerMapping执行这两项任务.因此,请求直接映射到方法.

有迹象表明,由这些标签(实例化等基础设施豆类除默认链接)一样- ,MappedInterceptor,,ConfigurableWebBindingInitializer 等我不打算解释这些:),因为他们每一个都长的答案本身,所以谷歌,以获得更多信息.SessionFlashManagerContentNegociationManager

PS:是的,Spring 3.1+会自动将@PathVariables暴露给模型.你也有mvc:interceptors标签.但我认为这与此无关<mvc:annotation-driven />.我强烈推荐阅读 - http://spring.io/blog/2009/12/21/mvc-simplifications-in-spring-3-0/


Ash*_*ena 7

要启用MVC Java配置,请将注释@EnableWebMvc添加到您的@Configuration类之一:

@Configuration
@EnableWebMvc
public class WebConfig {

}
Run Code Online (Sandbox Code Playgroud)

要在XML中实现相同的功能,请 在DispatcherServlet上下文中使用 mvc:annotation-driven元素(如果没有定义DispatcherServlet上下文,则在根上下文中使用):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
</beans>
Run Code Online (Sandbox Code Playgroud)

在上述寄存器一个RequestMappingHandlerMapping,一个RequestMappingHandlerAdapterExceptionHandlerExceptionResolver在支持与使用注释如注释的控制器方法的处理请求(除了别的以外)@RequestMapping,@ExceptionHandler,以及其他.

有关详情,请参阅以下链接:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config