mvc:annotation-driven標(biāo)簽詳解

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ā)請(qǐng)求
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路徑來映射請(qǐng)求處理器。

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,請(qǐng)求參數(shù)來映射處理類的方法。

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

HandlerAdaptor成為DispatcherServlet和Handler之間的“中間人”。

對(duì)比

<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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容