在上一篇部落格springMVC源碼分析--HandlerInterceptor攔截器(一)中我們介紹了HandlerInterceptor攔截器相關的内容,了解到了HandlerInterceptor提供的三個接口方法:
(1)preHandle: 在執行controller處理之前執行,傳回值為boolean ,傳回值為true時接着執行postHandle和afterCompletion,如果我們傳回false則中斷執行
(2)postHandle:在執行controller的處理後,在ModelAndView處理前執行
(3)afterCompletion :在DispatchServlet執行完ModelAndView之後執行
我們可以看到這三個方法的調用過程是不一樣的,接下來我們分析一下這個三個方法具體調用實作的地方。其最終實作調用的地方是在doDispatch函數中,因為doDispatch完成了一個請求到傳回資料的完整操作。
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerExecutionChain mappedHandler = null;
.......
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
.......
//擷取HandlerExecutionChain
mappedHandler = getHandler(processedRequest);
......
//最終會調用HandlerInterceptor的preHandle方法
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 調用具體的Controller中的處理方法Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
mappedHandler.applyPreHandle(processedRequest, response):最終會調用HandlerInterceptor的preHandle方法。在HandlerExecutionChain中的具體實作如下,我們可以看到會調用所有的HandlerInterceptor攔截器并調用其preHandler方法。
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();//擷取所有的攔截器
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(request, response, this.handler)) {//分别調用攔截器的preHandle方法
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
}
return true;
}
mappedHandler.applyPostHandle(processedRequest, response, mv):最終會調用HandlerInterceptor的postHandle方法
具體實作是在HandlerExecutionChain中實作如下,就是擷取所有的攔截器并調用其postHandle方法。
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();//擷取所有攔截器
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(request, response, this.handler, mv);//分别調用攔截器的postHandle方法
}
}
}
triggerAfterCompletion(processedRequest, response, mappedHandler, ex):最終會調用HandlerInterceptor的afterCompletion 方法
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err):
最終會調用HandlerInterceptor的afterCompletion 方法
private void triggerAfterCompletionWithError(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, Error error) throws Exception {
ServletException ex = new NestedServletException("Handler processing failed", error);
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, ex);
}
throw ex;
}
private void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, Exception ex) throws Exception {
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, ex);
}
throw ex;
}
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();//擷取所有攔截器
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = this.interceptorIndex; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
try {
interceptor.afterCompletion(request, response, this.handler, ex);//調用攔截器的afterCompletion方法
}
catch (Throwable ex2) {
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
}
}
}
}