When using Spring, I often had a lot of trouble if I didn't understand the processing flow to some extent, so I will summarize the contents of reading the source as my own memo.
DispatcherServlet All requests carry out the processing of this Servlet. To follow the process flow, I think you should look at the source from the doService method.
In order for this Servlet to call subsequent processing (Controller etc.) or to generate a response using JSP etc. You will need the following beans.
The general processing flow is as follows.
HandlerMapping Get the subsequent processing from the request with the getHandle method and return the HandlerExecutionChain. The HandlerExecutionChain can hold not only the handler (subsequent Controller method) but also multiple Interceptors. DispatcherServlet uses this HandlerExecutionChain to execute Interceptor and handler.
A class often used to implement this interface is the RequestMappingHanlderMapping class.
This class connects requests and processes based on the contents of @RequestMapping
of the Controller class.
HandlerAdapter Adatpter class for executing the handler obtained by HandlerMapping. Processes nicely before and after handler execution. (Roughly)
A class often used to implement this interface is the RequestMappingHandlerAdapter class. It stores request parameters in an object, validates them, and generates a ModelAndView from the return value of a handler. (Strictly speaking, processing is delegated to HandlerMethodArgumentResolver and HandlerMethodReturnValueHandler.)
HandlerExceptionResolver When an exception occurs, it connects the exception with subsequent processing.
A class often used to implement this interface is the ExceptionHandlerExceptionResolver class.
Methods with @ExceptionHandler
in Contoller and ControllerAdvice will be executed from this class.
By the way, as you can see by checking the contents, if there is a @ExceptionHandler
that handles the same exception in Controller and ControllerAdvice, the Controller side has priority.
ViewResolver You can get the View class from ViewName.
A class often used to implement this interface is the InternalResourceViewResolver class. This class can get JstlView for drawing JSP from ViewName. Also, if the prefix "redirect:" is set in ViewName, a RedirectView for redirect processing will be generated.
LocaleResolver It seems that you can get the Locale from the request. Since I have never used it, I do not know the details, but if you use SessionLocaleResolver, you can hold Locale information in Session, so it seems that you can manage Locale for each user.
ThemeResolver It seems that you can get Theme from the request. I haven't used Spring's Theme in the first place, so I don't know the details. ..
MultipartResolver An interface for generating a Multipart File from a multipart / form-data request. If you need a function such as file upload, you need to define this Resolver as a bean.
FlashMapManager An interface for managing FlashScope. FlashScope is a scope for holding values until the redirect destination. Normally, SessionFlashMapManager is used and Session is used to realize FlashScope. I have written about the mechanism here, so please refer to it if you like. Pass the value to the redirect destination without using Redirect Attributes in Spring MVC
Recommended Posts