DispathcerServlet initialization HandlerMapping, HandlerAdapter initialization

In the SpringMVC framework, DispatcherServlet is responsible for receiving all HTTP requests and distributing them to the corresponding processors. During the initialization process, DispatcherServlet will load and initialize all HandlerMapping and HandlerAdapter so that it can find the appropriate processor method and call it when processing the request.

The following are the steps for DispatcherServlet initialization HandlerMapping and HandlerAdapter, and are explained through code:

1. Load HandlerMapping: In SpringMVC, HandlerMapping is responsible for mapping requests to corresponding processor methods. During the initialization process, DispatcherServlet will load the HandlerMapping defined in the configuration file. For example, in an XML configuration file, you can use the <bean> element to define a HandlerMapping:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

In the above configuration, a BeanNameUrlHandlerMapping processor mapper is defined.

2. Initialize HandlerMapping: After loading HandlerMapping, DispatcherServlet will initialize all HandlerMapping. During the initialization process, all HandlerMappings will be traversed and corresponding HandlerExecutionChain objects will be created for them. The HandlerExecutionChain object is an object that encapsulates the handler method and interceptor chain, which is used to call the interceptor and handler methods in sequence when processing the request. For example, the following code demonstrates how to initialize a BeanNameUrlHandlerMapping object:

@Override  
public void initApplicationContext() throws BeansException {  
    super.initApplicationContext();  
    initHandlerMappings();  
}  
  
private void initHandlerMappings() {  
    for (HandlerMapping hm : this.handlerMappings) {  
        if (logger.isDebugEnabled()) {  
            logger.debug("Initializing " + hm + "");  
        }  
        hm.initApplicationContext();  
    }  
}

In the above code, the Spring container is first initialized by calling the initApplicationContext() method of the parent class. Then, initialize them by looping through all HandlerMapping objects in the handlerMappings collection and calling their initApplicationContext() method. In BeanNameUrlHandlerMapping, the initApplicationContext() method will map all Bean names and URL paths to the corresponding HandlerExecutionChain objects.

3. Load HandlerAdapter: In SpringMVC, HandlerAdapter is responsible for calling processor methods and processing return values. During the initialization process, DispatcherServlet will load the HandlerAdapter defined in the configuration file. For example, in an XML configuration file, you can use the <bean> element to define a HandlerAdapter:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

In the above configuration, a RequestMappingHandlerAdapter processor adapter is defined.

4. Initialize HandlerAdapter: After loading HandlerAdapter, DispatcherServlet will initialize all HandlerAdapter. During the initialization process, all HandlerAdapters will be traversed and corresponding processor adapter objects will be created for them. For example, the following code demonstrates how to initialize a RequestMappingHandlerAdapter object:

@Override  
public void initApplicationContext() throws BeansException {  
    super.initApplicationContext();  
    initHandlerAdapters();  
}  
  
private void initHandlerAdapters() {  
    for (HandlerAdapter ha : this.handlerAdapters) {  
        if (logger.isDebugEnabled()) {  
            logger.debug("Initializing " + ha + "");  
        }  
        ha.initApplicationContext();  
    }  
}

 In the above code, the Spring container is first initialized by calling the initApplicationContext() method of the parent class. Then, initialize them by looping through all HandlerAdapter objects in the handlerAdapters collection and calling their initApplicationContext() method. In RequestMappingHandlerAdapter, the initApplicationContext() method will map all RequestMappingInfo objects and HandlerMethod objects to the corresponding processor adapter objects. In this way, when processing a request, you can find the corresponding processor adapter object through the RequestMappingInfo object, and call the corresponding processor method through the processor adapter object.

おすすめ

転載: blog.csdn.net/weixin_52721608/article/details/133491350