可以在Controller上打断点,跟踪Springboot的执行流程,如下图
过滤器(Filter)和拦截器(Interceptor)的代码示例,参见《Springboot Filter Interceptor》
Filter主要用法
前端控制器(DispatcherServlet),相当于 MVC 模式中的 C,是整个流程控制的中心,由它调用其它组件处理用户的请求。DispatcherServlet 的存在降低了组件之间的耦合性,提高了系统的可扩展性。
核心代码
/**
* Process the actual dispatching to the handler.
* The handler will be obtained by applying the servlet's HandlerMappings in order.
* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
* to find the first that supports the handler class.
*
All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
* themselves to decide which methods are acceptable.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
*/
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
处理器映射器(HandlerMapping),负责根据用户请求的 url 找到 请求处理器(Handler),SpringMVC提供了不同的映射器来实现不同的映射方式,根据一定的规则去查找,例如:xml配置方式,接口方式,注解方式等。
请求处理器(Handler),是继 DispatcherServlet 的后端控制器,在 DispatcherServlet 的控制下 Handler 对具体的用户请求进行处理。由于 Handler 涉及到具体的用户业务请求,所以一般需要程序员根据业务需求开发Handler(Controller)。
处理器适配器(HandlAdapter),负责调用请求处理器执行业务逻辑,通过扩展适配器可以执行更多类型的请求处理器,这是适配器模式的应用。
ModelAndView 是 SpringMVC 的封装对象,它将 Model 和 View 封装在一起。
视图解析器(ViewResolver),负责将处理结果生成 View 视图,视图解析器首先根据逻辑视图名解析成物理视图名,即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
模型数据(Model),是用于填充视图的数据模型,一般需要程序员根据业务需求开发。
视图(View),是springmvc的封装对象,是一个接口,SpringMVC 框架提供了很多的 View 视图类型,包括:jspview,pdfview, jstlView、freemarkerView、pdfView、ThemleafView 等。一般情况下需要通过页面标签或页面模版技术,将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
参考文献
SpringMVC执行流程及工作原理
Spring中Filter和Interceptor的区别