美文网首页
mvc:annotation-driven标签详解

mvc:annotation-driven标签详解

作者: 缄默的石头 | 来源:发表于2017-04-22 21:29 被阅读527次

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.

相关文章

网友评论

      本文标题:mvc:annotation-driven标签详解

      本文链接:https://www.haomeiwen.com/subject/yhfezttx.html