Spring MVC之<mvc:annotation-driven/>

整理文章:http://blog.csdn.net/sjjsh2/article/details/53100728

 一、简短的介绍

   <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的,即解决了@Controller注解使用的前提配置。

  同时它还提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB,读写JSON的支持(Jackson)。我们处理响应ajax请求时,就使用到了对json的支持(配置之后,在加入了jackson的core和mapper包之后,不写配置文件也能自动转换成json)。

  而且,当对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道正是<mvc:annotation-driven />这一句注册的这两个bean。

 

  二、与<context:component-scan/>的区别

  <context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。
而<mvc:annotation-scan/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。

 三、与<context:annotation-config/>的区别

  当我们需要使用注解模式时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

[html]  view plain  copy
  1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>  
  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:
[html]  view plain  copy
  1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
  简单的说,用什么注解,就需要声明对应的BeanPostProcessor。这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。

    另外,在我们使用注解时一般都会配置扫描包路径选项,即<context:component-scan/>。该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去,但必须要配置全!以防万一,还是同时声明的好。

二与<context:component-scan/>的区别

  <context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。
而<mvc:annotation-scan/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。

  三、与<context:annotation-config/>的区别

  当我们需要使用注解模式时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

[html]  view plain  copy
  1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>  
  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:
[html]  view plain  copy
  1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
  简单的说,用什么注解,就需要声明对应的BeanPostProcessor。这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。

    另外,在我们使用注解时一般都会配置扫描包路径选项,即<context:component-scan/>。该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去,但必须要配置全!以防万一,还是同时声明的好。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值