0%

HandlerAdapter

HandlerAdapter处理适配器

HandlerMapping通过request找到了handler,HandlerAdapter是具体使用Handler来干活的,每个HandlerAdapter封装了一种Handler的具体使用方法

由于Spring经过很长时间的版本迭代,为了适配老版本,Spring 中的处理器的实现有很多种方式,比如可以实现 Controller 接口,也可以用 @Controller+@RequestMapping 注解将方法作为一个处理器等,这就导致 Spring 不知道怎么调用用户的处理器逻辑。因此需要一个处理器适配器,由处理器适配器去调用处理器的逻辑

HandlerAdapter按照特定规则去执行Handler,通过扩展适配器可以对更多类型的处理器进行执行

1
2
3
4
5
6
7
8
9
10
11
12
public interface HandlerAdapter {

// 判断是否可以使用某个Handler
boolean supports(Object handler);

// 具体使用handler完成工作
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

// 获取Last-Modified资源最后一次修改时间
long getLastModified(HttpServletRequest request, Object handler);

}
1
2
<bean id="annotationMethodHandlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  • HttpRequestHandlerAdapter 用于适配HttpRequestHandler,处理实现了HttpRequestHandler接口的handler

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class HttpRequestHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
    return (handler instanceof HttpRequestHandler);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {

    ((HttpRequestHandler) handler).handleRequest(request, response);
    return null;
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
    if (handler instanceof LastModified) {
    return ((LastModified) handler).getLastModified(request);
    }
    return -1L;
    }

    }
  • SimpleControllerHandlerAdapter 用于适配Controller,处理实现了Controller接口的handler

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class SimpleControllerHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
    return (handler instanceof Controller);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {

    return ((Controller) handler).handleRequest(request, response);
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
    if (handler instanceof LastModified) {
    return ((LastModified) handler).getLastModified(request);
    }
    return -1L;
    }

    }
  • SimpleServletHandlerAdapter 用于适配Servlet,处理实现了Servlet接口的handler

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class SimpleServletHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
    return (handler instanceof Servlet);
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {

    ((Servlet) handler).service(request, response);
    return null;
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
    return -1;
    }

    }
  • RequestMappingHandlerAdapter 用于适配HandlerMethod,这个就比较复杂了,下边单独拿出来说明

RequestMappingHandlerAdapter

平常使用最多的也是RequestMappingHandlerAdapter,可以看到它是处理HandlerMethod类的

1
2
3
public final boolean supports(Object handler) {
return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));
}

就是使用@Controller和@RequestMapping来进行映射的方法

该类实现了InitializingBean接口,会执行afterPropertiesSet方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void afterPropertiesSet() {
// Do this first, it may add ResponseBody advice beans
// @ControllerAdvice的bean
initControllerAdviceCache();

if (this.argumentResolvers == null) {
// 一些参数解析器,如解析@RequestParam、@PathVariable、@RequestBody等
List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
}
if (this.initBinderArgumentResolvers == null) {
// initBinder的参数解析器
List<HandlerMethodArgumentResolver> resolvers = getDefaultInitBinderArgumentResolvers();
this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
}
if (this.returnValueHandlers == null) {
// 返回值的参数解析器,如ModelAndView、ResponseEntity、HttpEntity、@ResponseBody注解等
List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
}
}

处理请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
protected ModelAndView handleInternal(HttpServletRequest request,
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

ModelAndView mav;
checkRequest(request);

// Execute invokeHandlerMethod in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
mav = invokeHandlerMethod(request, response, handlerMethod);
}
}
else {
// No HttpSession available -> no mutex necessary
mav = invokeHandlerMethod(request, response, handlerMethod);
}
}
else {
// No synchronization on session demanded at all...
// 具体的执行逻辑
mav = invokeHandlerMethod(request, response, handlerMethod);
}
// 处理缓存
if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
}
else {
prepareResponse(response);
}
}

return mav;
}
invokeHandlerMethod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
protected ModelAndView invokeHandlerMethod(HttpServletRequest request,
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

ServletWebRequest webRequest = new ServletWebRequest(request, response);
try {
// 将注解@InitBinder的方法找出来
WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
// 用来处理Model的,在处理器具体处理之前对Model进行初始化,在处理万请求之后对Model参数进行更新
ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
// 用于参数绑定、处理请求以及返回值处理
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
invocableMethod.setDataBinderFactory(binderFactory);
invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
// 用于保存Model和View的
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
// 先将FlashMap中的数据添加进ModelAndViewContainer中
mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
// 初始化Model,处理@SessionAttributes注解和WebDataBinder定义的全局数据
modelFactory.initModel(webRequest, mavContainer, invocableMethod);
mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
// 异步请求
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
asyncWebRequest.setTimeout(this.asyncRequestTimeout);

WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.setTaskExecutor(this.taskExecutor);
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.registerCallableInterceptors(this.callableInterceptors);
asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);

if (asyncManager.hasConcurrentResult()) {
Object result = asyncManager.getConcurrentResult();
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
asyncManager.clearConcurrentResult();

invocableMethod = invocableMethod.wrapConcurrentResult(result);
}
// 执行请求
invocableMethod.invokeAndHandle(webRequest, mavContainer);
if (asyncManager.isConcurrentHandlingStarted()) {
return null;
}
// 请求执行完之后进行处理
// ①modelFactory.updateModel 更新Model
// ②根据mavContainer创建modelAndView
// ③如果model是RedirectAttributes类型,需要设置到FlashMap中
return getModelAndView(mavContainer, modelFactory, webRequest);
}
finally {
webRequest.requestCompleted();
}
}

欢迎关注我的其它发布渠道