mvc:annotation-driven標簽詳解

mvc:annotation-driven標簽

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.
這個標簽本質上設置Spring上下文允許為Controller轉發請求
The tag will configure two beans DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter.
這個標簽會自動配置兩個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.
這個Bean實現了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.
這個Bean實現了HandlerAdaper接口,通過@RequestMappingHttp上的Http路徑,HttpMethod,請求參數來映射處理類的方法。

DispatcherServlet在使用HandleMapping返回HandlerExecutionChain,HandlerExecutionChain包含的是一個Handler類型的對象,但是沒有限定它的具體類型,只要能處理web請求就行了,不一定是Controller類型。對于DispatcherServlet,它不知道如何判斷Handler的類型,以及調用Handler的什么方法來處理請求,硬編碼是不合適的,也沒辦法枚舉出所有的類型(開發者可以自定義Handler類型),為了屏蔽不同Handler之間的差異,因此DispatcherServlet將Handler的調用交給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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 1.Spring web mvc介紹 Spring web mvc和Struts2都屬于表現層的框架,它是Spri...
    七弦桐語閱讀 11,558評論 2 38
  • 星期五要考試,一個同等于高考一樣重要的考試又要來了。心里實在沉不住氣,但是又一定要保持淡定。很多時候我都輸在粗心上...
    kriskrisseagull閱讀 167評論 0 0
  • http://www.cnblogs.com/cqb-learner/p/5894475.html oschina...
    xmb閱讀 436評論 0 0