All the time to meet, are long separation reunion — the Grandmaster
preface
I haven’t written a blog for half a month. I’ve been thinking about writing a backend blog lately
The java-related backend frameworks that have a strong presence are Jfinal and SpringBoot. Since Jfinal is a domestic framework, there was also an attachment in my heart at that time, so I roughly went through the source code. In the subsequent projects, AS long as there was no technical stack limit, I generally used Jfinal, but gradually found that there was still a gap between ecology and SpringBoot, in order to shorten the project development cycle, And then changed strategy to look at SpringBoot. Ecology is really important! It’s not really a matter of skill.
When you don’t touch SpringBoot, Spring actually plays ok, but still wants to mess around. Understand, skilled, proficient, do not look at the source code how can be considered proficient 😆😆😆
How does a request get forwarded from Tomcat to SpringBoot, and how does a request get processed in SpringBoot? Look through the source code, it is not difficult, but you want to understand the extension and development of great use. Prefer to use SpringBoot’s own utility classes.
Source code only about the logic, after all, not write framework, there is no need to see word for word
How does a request get from Tomcat to SpringBoot
Tomcat finds the corresponding Servlet according to the Request, generates the corresponding FilterChain based on the Servlet and Request, and then executes Filter and Servlet in responsibility chain mode to process the Request logic.
StandardWrapperValve.invoke(Request request, Response response)
// Assign a servlet instance to handle the request
if(! unavailable) { servlet = wrapper.allocate(); }Copy the code
- Code instructions
@Slf4j
@WebServlet(name = "MyServletJson", urlPatterns = "/servlet/json", description = "Servlet Specification")
@Component
public class MyServletJson extends HttpServlet {}Copy the code
When the request URL is /servlet/json, servlet = wrapper.allocate(); The assigned instance is MyServletJson
Url does not exactly match the registered Servlet, Servlet = wrapper.allocate(); The DispatcherServlet instance is assigned
Create a FilterChain
// Create a filter chain for the current request
ApplicationFilterChain filterChain =
ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);
Copy the code
ApplicationFilterChain doFilter filter chain execution
Executing this method executes the filter and the corresponding Servlet in chain mode
filterChain.doFilter(request.getRequest(), response.getResponse());
Copy the code
The servlet.service method is executed when the last filter chain is executed
ApplicationFilterChain.doFilter(ServletRequest request, ServletResponse response){ internalDoFilter(request,response){ servlet.service(request, response); }}Copy the code
The above code flow shows how requests go from Tomcat to the SpringBoot framework. Take a quick look at the code to understand the flow. Our focus should be on SpringBoot.
How does SpringBoot handle requests
In the Tomcat servlet service (request, response); The request is forwarded to a Servlet (DispatcherServlet)
Idea in the view hierarchy, our main concern is the HttpServlet, FrameworkServlet, DispatcherServlet.
To debug to see code, the final processing logic focused on the DispatcherServlet. DoDispatch
DispatcherServlet.doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine the handler execution chain of the current request (determine which interceptors the current request executes)
HandlerExecutionChain mappedHandler=getHandler(processedRequest);
// Determine the processor adapter for the current request based on handler.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Execute the preHandle method in the interceptor, the chain of responsibility executes, one of which returns false, and the corresponding afterHandle chain executes
if(! mappedHandler.applyPreHandle(processedRequest, response)) {return;
}
// Implement AOP and controller,service, DAO,
// In this logic, Jackson writes data to response and returns mv=null;
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// Set the default View in ModelAndView
applyDefaultViewName(processedRequest, mv);
// Chain execution interceptor postHandle
mappedHandler.applyPostHandle(processedRequest, response, mv);
// view. render writes data to Response
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
// afterHandle in chain execution interceptor
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
Copy the code
Graph also can’t draw, direct online to find a 😂😂😂
We can also wrap our own View in view. render. For common file downloads, we can encapsulate our own View, just need to pass in the corresponding file stream and file name to achieve the purpose of download
Get the HandlerExecutionChain from HandlerMapping
HandlerExecutionChain mappedHandler=getHandler(processedRequest);
Commonly used HandlerMapping is:
- RequestMappingHandlerMapping
Handle restful interfaces. The corresponding handler is HandlerMethod
We can use the RequestMappingHandlerMapping url corresponding HandlerMethod, HandlerMethod methods and classes can be obtained on the annotation based interface to access data initialization, from manual data
- SimpleUrlHandlerMapping
Forward or redirect links, in this configuration WebMvcConfigurer. AddViewControllers (); A handler for the ParameterizableViewController
- SimpleUrlHandlerMapping is used to process static resources
Processing page or static resource handler for ResourceHttpRequestHandler
Get processor adapters by processor
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
Common HandlerAdapters are:
- RequestMappingHandlerAdapter
Handle restful interfaces. Handler is HandlerMethod
- SimpleControllerHandlerAdapter
Processing simple url forwarding or redirect, WebMvcConfigurer. AddViewControllers (); A handler for the ParameterizableViewController
- HttpRequestHandlerAdapter
Deal with static resources, ResourceHttpRequestHandler handler