springmvc 註解使用的小總結

1 <context:annotation-config />

作用:註冊AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor

註冊這4 BeanPostProcessor的作用,就是爲了你的系統能夠識別相應的註解.

2 <context:component-scan base-package=""/>:包含了context:annotation-config 的功能,同時能指定掃描路徑。因此當使用 <context:component-scan/> 後,除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor兩個Processor的功能(例如JPA等)否則就可以將 <context:annotation-config/> 移除了。

3 <mvc:annotation-driven />

DefaultAnnotationHandlerMapping
用來解析Spring MVC裏面的annotation對應的Controller,也就是通過這個類,給annotation設置映射關係,如@RequestMapping等,主要方法determineUrlsForHandlerMethods(Class<?> handlerType, final boolean hasTypeLevelMapping)
AnnotationMethodHandlerAdapter
對Annotation搜索設置的方法進行處理的類,通過此類,解析annotation設置的類的處理,也就是有請求時,通過此類,可以調用annotation設置controller的方法,主要處理方法,handle(HttpServletRequest req, HttpServletResponse resp, Object handler)。

<mvc:annotation-driven/>相當於註冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller註解的使用前提配置。






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