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阅读 135,403评论 19 139
  • 1.Spring web mvc介绍 Spring web mvc和Struts2都属于表现层的框架,它是Spri...
    七弦桐语阅读 13,957评论 2 38
  • 星期五要考试,一个同等于高考一样重要的考试又要来了。心里实在沉不住气,但是又一定要保持淡定。很多时候我都输在粗心上...
    kriskrisseagull阅读 1,144评论 0 0
  • http://www.cnblogs.com/cqb-learner/p/5894475.html oschina...
    xmb阅读 3,110评论 0 0