Spring MVC之HandlerMapping

Spring MVC之HandlerMapping

 

Spring's HandlerMapping, to put it bluntly, is to find which object and which method to access according to the URL, put these into the Handler object, and then put the Handler and the interceptor into the HandlerExecutionChain object.

 

The use of HandlerMapping is mainly divided into two steps: registration and search

 

Registration is to store a string and a Controller class in the Map in the form of <key, value> according to the configuration in the configuration file. The key is a field in the corresponding url.

 

The search is that HandlerMapping uses a field in the url as the corresponding Controller class in the Map, and encapsulates the Controller class into a HandlerExecutionChain object. In addition to the Controller object, HandlerExecutionChain also has a set of interceptors.

 

 

 

What HandlerMapping returns is a HandlerExecutionChain object that contains:

 

1. Handler (which contains beans)

 

2. A set of interceptors required by the bean

 

 

Implementation of the HandlerMapping interface

 

BeanNameUrlHandlerMapping : Find the corresponding object by comparing the url and the name of the bean 

SimpleUrlHandlerMapping: It also directly configures the url and the corresponding bean, which has more functions than BeanNameUrlHandlerMapping 

DefaultAnnotationHandlerMapping : mainly for annotation configuration @RequestMapping, outdated 

RequestMappingHandlerMapping : replaces the above one 

 

 

Summarize

 

The role of handlerMapping

handlerMapping finds the controller that executes the request according to the user request.

 

HandlerMapping finds the controller process

      1. 根据配置文件对url到controller的映射进行注册

      2. 根据具体的url请求找到执行该请求的controller

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326496003&siteId=291194637