1 <context:annotation-config />
作用:註冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 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註解的使用前提配置。