天天看點

SpringMVC源碼解析之ServletInvocableHandlerMethodInvocableHandlerMethodServletInvocableHandlerMethod

InvocableHandlerMethod

提供了一種方法,用于調用處理器方法,處理給定的請求,其已認證注冊的HandlerMethodArgumentResolver解析了方法參數值。

參數解析往往需要WebDataBinder用于資料結合或進行類型轉換。使用setDataBinderFactory(WebDataBinderFactory)屬性來提供一種粘合劑廠傳遞給參數解析器。

使用setHandlerMethodArgumentResolvers自定義的參數解析器的清單。

invokeForRequest

解析給定請求的上下文中其參數值後調用指定方法。

參數值是通過HandlerMethodArgumentResolver解析的。

SpringMVC源碼解析之ServletInvocableHandlerMethodInvocableHandlerMethodServletInvocableHandlerMethod

doInvoke

調用與給定的參數值的處理方法

protected Object doInvoke(Object... args) throws Exception {
        ReflectionUtils.makeAccessible(getBridgedMethod());
        try {
            return getBridgedMethod().invoke(getBean(), args);
        }
        catch (IllegalArgumentException ex) {
            assertTargetBean(getBridgedMethod(), getBean(), args);
            String text = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument");
            throw new IllegalStateException(getInvocationErrorMessage(text, args), ex);
        }
        catch (InvocationTargetException ex) {
            // Unwrap for HandlerExceptionResolvers ...
            Throwable targetException = ex.getTargetException();
            if (targetException instanceof RuntimeException) {
                throw (RuntimeException) targetException;
            }
            else if (targetException instanceof Error) {
                throw (Error) targetException;
            }
            else if (targetException instanceof Exception) {
                throw (Exception) targetException;
            }
            else {
                String text = getInvocationErrorMessage("Failed to invoke handler method", args);
                throw new IllegalStateException(text, targetException);
            }
        }
    }      

ServletInvocableHandlerMethod

SpringMVC源碼解析之ServletInvocableHandlerMethodInvocableHandlerMethodServletInvocableHandlerMethod

擴充InvocableHandlerMethod通過注冊的能力來處理傳回值HandlerMethodReturnValueHandler并且還支援設定基于方法級響應狀态@ResponseStatus注解。

甲null傳回值(包括空隙)可以被解釋為請求處理結束結合有@ResponseStatus注釋,未改性的檢查條件(見ServletWebRequest.checkNotModified(long) ),或提供對所述接入的方法的參數響應流

invokeAndHandle

調用該方法,并通過所配置的HandlerMethodReturnValueHandler處理傳回值

  • WebRequest - 目前請求
  • mavContainer - 在ModelAndViewContainer此請求
  • providedArgs - “給”論據類型比對(未解析)
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
        Object... providedArgs) throws Exception {

    Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
    setResponseStatus(webRequest);

    if (returnValue == null) {
        if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
            mavContainer.setRequestHandled(true);
            return;
        }
    }
    else if (StringUtils.hasText(getResponseStatusReason())) {
        mavContainer.setRequestHandled(true);
        return;
    }

    mavContainer.setRequestHandled(false);
    try {
        this.returnValueHandlers.handleReturnValue(
                returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
    }
    catch (Exception ex) {
        if (logger.isTraceEnabled()) {
            logger.trace(getReturnValueHandlingErrorMessage("Error handling return value", returnValue), ex);
        }
        throw ex;
    }
}      

繼續閱讀