Spring MVC源码解析:HandlerMapping、HandlerAdapter的实现原理

魔法少女 2024-04-23 ⋅ 12 阅读

在Spring MVC框架中,HandlerMapping和HandlerAdapter是两个重要的接口,负责将请求映射到具体的处理器方法,并执行处理器方法进行业务逻辑处理。本文将通过源码解析的方式,深入探讨这两个接口的实现原理。

HandlerMapping

HandlerMapping接口的主要作用是根据请求的URL找到对应的处理器方法,并返回一个HandlerExecutionChain对象,其中包含了处理器对象和处理器方法。Spring MVC提供了多种HandlerMapping的实现类,例如RequestMappingHandlerMapping、SimpleUrlHandlerMapping等。这些实现类都继承自AbstractHandlerMethodMapping类。

public interface HandlerMapping {
    HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}

RequestMappingHandlerMapping

RequestMappingHandlerMapping是Spring MVC中最常用的HandlerMapping实现类,它通过@RequestMapping注解来映射处理器方法。在RequestMappingHandlerMapping中,主要有两个核心方法来处理请求映射的逻辑:

  • detectHandlerMethods:扫描所有的Controller类,解析其中带有@RequestMapping注解的方法,构建MethodRequestMappingInfo映射信息。
  • lookupHandlerMethod:根据请求的URL进行匹配查找处理器方法。
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping {
    
    @Override
    protected void detectHandlerMethods(Object handler) {
        Class<?> handlerType = (handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass());
        if (handlerType != null) {
            Map<Method, T> methods = MethodIntrospector.selectMethods(handlerType, (MethodIntrospector.MetadataLookup<T>) method -> {
                T mapping = getMappingForMethod(method, handlerType);
                return (mapping != null ? method : null);
            });
            if (!methods.isEmpty()) {
                this.mappingRegistry.register(handler, methods, handlerType);
            }
        }
    }
    
    @Override
    protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
        return this.mappingRegistry.getHandlerMethod(lookupPath, request);
    }
}

HandlerAdapter

HandlerAdapter接口的主要作用是执行处理器方法,负责将请求参数绑定到方法的参数上,并调用方法进行业务逻辑处理。Spring MVC提供了多种HandlerAdapter的实现类,例如RequestMappingHandlerAdapter、HttpRequestHandlerAdapter等。这些实现类都继承自AbstractHandlerMethodAdapter类。

public interface HandlerAdapter {
    ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
}

RequestMappingHandlerAdapter

RequestMappingHandlerAdapter是Spring MVC中最常用的HandlerAdapter实现类,它通过反射的方式调用处理器方法,将请求参数绑定到方法的参数上。RequestMappingHandlerAdapter的主要处理逻辑如下:

  • handleInternal:根据请求的Handler去执行对应的HandlerMethod。
  • invokeHandlerMethod:通过反射调用处理器方法,处理请求参数绑定、结果返回等。
public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter {

    @Override
    protected ModelAndView handleInternal(HttpServletRequest request,
            HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
        InvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
        if (this.argumentResolvers != null) {
            invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
        }
        if (this.returnValueHandlers != null) {
            invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
        }
        return invocableMethod.invokeAndHandle(request, response);
    }

    private InvocableHandlerMethod createInvocableHandlerMethod(HandlerMethod handlerMethod) {
        return new InvocableHandlerMethod(handlerMethod);
    }
}

通过对HandlerMapping和HandlerAdapter的源码解析,我们可以更深入地了解Spring MVC框架中请求映射和处理逻辑的实现原理,对于开发者来说也有助于更好地掌握Spring MVC框枋的相关知识。希望本文可以帮助读者对Spring MVC源码有更深入的理解和掌握。


全部评论: 0

    我有话说: