mvc:annotation-driven標(biāo)簽
Description : Configures the annotation-driven Spring MVC Controller programming model. Note that this tag works in Web MVC only, not in Portlet MVC!
See org.springframework.web.servlet.config.annotation.EnableWebMvc javadoc for details on code-based alternatives to enabling annotation-driven Spring MVC support.
來自Stackoverflow
The mvc:annotationDriven tag essentially sets you your Spring context to allow for dispatching requests to Controllers.
這個(gè)標(biāo)簽本質(zhì)上設(shè)置Spring上下文允許為Controller轉(zhuǎn)發(fā)請求
The tag will configure two beans DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter.
這個(gè)標(biāo)簽會(huì)自動(dòng)配置兩個(gè)bean,分別是DefaultAnnotatationHandlerMapping和AnnotataionMethodHanderAdapter.
DefaultAnnotationHanderMapping
Implementation of the {@link org.springframework.web.servlet.HandlerMapping}interface that maps handlers based on HTTP paths expressed through the {@link RequestMapping} annotation at the type or method level.
這個(gè)Bean實(shí)現(xiàn)了HandlerMapping接口,通過@RequestMapping注解上的Http路徑來映射請求處理器。
AnnotataionMethodHanderAdapter
Implementation of the {@link org.springframework.web.servlet.HandlerAdapter} interface
that maps handler methods based on HTTP paths, HTTP methods and request parameters expressed through the {@link RequestMapping} annotation.
這個(gè)Bean實(shí)現(xiàn)了HandlerAdaper接口,通過@RequestMappingHttp上的Http路徑,HttpMethod,請求參數(shù)來映射處理類的方法。
DispatcherServlet在使用HandleMapping返回HandlerExecutionChain,HandlerExecutionChain包含的是一個(gè)Handler類型的對象,但是沒有限定它的具體類型,只要能處理web請求就行了,不一定是Controller類型。對于DispatcherServlet,它不知道如何判斷Handler的類型,以及調(diào)用Handler的什么方法來處理請求,硬編碼是不合適的,也沒辦法枚舉出所有的類型(開發(fā)者可以自定義Handler類型),為了屏蔽不同Handler之間的差異,因此DispatcherServlet將Handler的調(diào)用交給HandlerAdaptor類型。
HandlerAdaptor成為DispatcherServlet和Handler之間的“中間人”。
對比
<context:annotation-config> declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.
<mvc:annotation-driven /> declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declarative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.