spring-config.xml的annotation-driven详解

使用spring mvc时,经常有这个注解annotation-driven,意思是支持注解。
例:
< context:annotation-config/>
支持配置注解
向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
比如支持以下注解:
@Autowired注解,相当于在 Spring 容器中声明AutowiredAnnotationBeanPostProcessor

@ Resource 、@ PostConstruct、@ PreDestroy等注解,相当于声明CommonAnnotationBeanPostProcessor的Bean。
@PersistenceContext注解,相当于声明PersistenceAnnotationBeanPostProcessor的Bean。
@Required的注解,相当于声明RequiredAnnotationBeanPostProcessor的Bean。
这些注解是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供< context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,我们使用注解一般都会配置扫描包路径选项
< context:component-scan base-package=”XX.XX”/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用 < context:component-scan/> 后,就可以将 < context:annotation-config/> 移除了。

< mvc:annotation-driven/>
支持MVC注解,在controller中使用mvc的各种注解。
自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
比如支持以下注解:
@NumberFormatannotation ,@DateTimeFormat ,@Valid
读写JSON的支持(Jackson)的@JsonIgnore
读写XML的支持(JAXB)

< tx:annotation-driven transaction-manager=“txManager”/>
支持事务注解
指定使用txManager事务管理器。

< task:annotation-driven executor=“executor” scheduler=“scheduler” />
支持定时任务注解

参考文章:
https://www.cnblogs.com/dreamroute/p/4493346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值