SpringMVC source code parsing -doDispatch method

Break entrance

After the container starts, what do we do with our Diskerservlets based on the URL provider?

We can look at it in detail by breaking the dot in the doDispatch method in the DiskcherServlet class;

Why interrupt the dot directly in the doDispatch method?

As can be seen from the figure, the Dispatcherservlet inherits from the HttpServlet abstract class, and our get/post request requests its doGet and doPost methods respectively;

Both the DoGet and doPost methods are overridden in the DispatcherServlet’s parent, FrameworkServlet, and both execute this.processRequest(request, response);

    protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);

    protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);

The processRequest method executes the method: this.doService(request, response); While the doService in the FrameworkServlet is abstract, the concrete implementation is in the DistributorServlet;

    protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception;

The doService in the PatchcherServlet implements the key method: this.doDispatch(request, response).

So we do the breakpoint at this method and see what the Patchcherservlet does.

DoDispatch method

Foreword: In addition to creating a new class using @Controller annotations, what other way can you create a new Controller?
It is also possible to implement the Controller interface or to implement the HttpRequestHandler interface, override its handRequest method, and don’t forget to use the Complex (“/URI”) annotation.

To doDispatch, look directly at the main method: getHandler

Spring is to find Handlers (that is, Controller) through HandlerMapping objects, and to find Controlrs created in different ways through different HandlerMapping implementations;

By breakingpoints, you can see that there are two implementations of HandlerMapping by default:
RequestMappingHandlerMapping and BeanNameUrlHandlerMapping;

All Controller objects with @Controller will be placed in
a Map property of The Parent Class of RequestMappingHandlerMapping, AbstractHandlerMethodMapping’ internal class, MappingRegistry.

So the Controller object defined in the form of BeanName will be placed in the Map property of The Parent Class of The RobotUrlHandlerMapping of The PredecessorTalkMapping.

    class MappingRegistry {
        private final MultiValueMap<String, T> pathLookup = new LinkedMultiValueMap();
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping implements MatchableHandlerMapping {
    private final Map<String, Object> handlerMap = new LinkedHashMap();

Return a HandleLerExecutionChain object encapsulating Handler through the HandlerMapping .getHandler(request) method;

Then execute the getHandlerAdapter method to obtain its corresponding HandlerAdapter adapter through Handler;

HandlerAdapter also has multiple implementations, according to different Handlers, through the supports method to determine which class implementation of the Handleer is, to return different adapter implementations;

    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
        if (this.handlerAdapters != null) {
            Iterator var2 = this.handlerAdapters.iterator();

            while(var2.hasNext()) {
                HandlerAdapter adapter = (HandlerAdapter)var2.next();
                if (adapter.supports(handler)) {
                    return adapter;
        throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");

Then the HandleErAdapter.handle method will be executed, which will call the specific business of the Controller according to the adapter, and different adapters will handle the corresponding handle in different ways;

Finally, the ModelAndView object is returned.

Be the first to comment

Leave a Reply

Your email address will not be published.