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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章