SpringMVC-mvc:annotation-driven-底层做了啥?

  • 基于注解的MVC在Spring 2.5中引入了该框架。该模型使得开发人员可以将任何POJO用作控制器,并且具有处理程序签名的灵活性。从Spring 3.0开始不推荐使用旧的控制层次结构。在将来的版本之一是。它将从发行版中完全删除。

  • <mvc:annotation-driven> 标记应添加到Web应用程序上下文XML。此标记默认为将请求委派给控制器所需的基本组件。

  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     
        <mvc:annotation-driven  />
     
    </beans>
    
  • 如果未将此标签添加到XML中,那么您将必须为HandlerAdapter, HandlerMapping, Binding Initializer, RequestMessage 转换器等组件手动定义Bean。 此标签帮助注册以下组件。

  1. DefaultAnnotationHandlerMapping : 这是一个HandlerMapping实现,它将HTTP请求映射到使用@RequestMapping注解定义的处理程序的方法。
  2. AnnotationMethodHandlerAdapter: 它负责扫描控制器以标记以@MVC注解标记的方法和参数,它扫描并缓存以@RequestMapping注解的处理程序方法。还处理@RequestParam, @ModelAttribute, @SessionAttributes 和 @InitBinder批注
  3. ConfigurableWebBindingInitializer: Web数据绑定器的初始化程序。帮助通过声明器,转换服务,属性编辑器等声明性第配置Web Binder.
  4. LocalValidatorFactoryBean: 实现验证接口并启用JSR303验证。这被注入到ConfigurableWebBindingInitializer中。
  5. FormattingConversionServiceFactoryBean: 转换工厂,返回基本对象(如日期和数字)的转换服务,该工厂再次被注入到ConfigurableWebBindingInitializer中
  6. 消息转换器
    • ByteArrayHttpMessageConverter: Http请求消息转换器,它读取HTTP消息主题并返回字节流。它还可以读取字节流并构造响应主体。用于接收和发送PDF,XLS等文档
    • StringHttpMessageConverter : Http请求消息转换器,它读取纯文本请求主体并将其绑定到String对象,反之亦然。
    • FormHttpMessageConverter: Http请求消息转换器,它读取表单编码的请求主体并绑定到表单绑定对象。
    • SourceHttpMessageConverter: 请求转换器,用于将XML消息主题与绑定对象之间进行转换。
  • 如果这些bean是在XML中定义的,而不是使用**<mvc:annotation-driven>** 则看起来将像这样。

  • <beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     
     
     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
      <property name="order" value="0">
     </property></bean>
     
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="webBindingInitializer">
       <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="validator" ref="validator">
       </property></bean>
      </property>
      <property name="messageConverters">
       <list>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <bean class="org.springframework.http.converter.FormHttpMessageConverter">
        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter">
       </bean></bean></bean></bean></list>
      </property>
     </bean>
     
     <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" id="validator">
     <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversion-service">
     
    </bean></bean></beans>
    

原文地址:https://www.codelooru.com/2010/10/what-does-mvcannotation-driven-do.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值