Adapter pattern HandlerAdapter in Spring framework
The Adapter pattern HandlerAdapter in the Spring framework is used to adapt different types of processors (Handlers) and the calling methods of DispatcherServlet. According to different processor types, Spring will use different HandlerAdapters to adapt the calling method of the processor to the calling method of the DispatcherServlet.
HandlerAdapter is an interface that defines the methods of a processor adapter, including determining whether the processor supports it, executing the processor, and so on. There are multiple different types of HandlerAdapter implementation classes in the Spring framework, including SimpleControllerHandlerAdapter, HttpRequestHandlerAdapter, HandlerExceptionResolverAdapter, and so on.
The following is the complete source code of the HandlerAdapter interface in the Spring framework:
public interface HandlerAdapter {
boolean supports(Object handler);
@Nullable
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
}
The core idea of the Adapter pattern is to unify different types of processors into a standard calling method through the adapter, so that the caller does not need to care about the specific type of processor, and directly uses the adapter to call. In the Spring framework, the HandlerAdapter plays this role by adapting different types of processors to the calling methods of DispatcherServlet, enabling unified processing of different types of processors in DispatcherServlet.
Summary:
1. HandlerAdapter is one of the applications of the Adapter pattern in the Spring framework, which is used to adapt different types of processors to the calling method of DispatcherServlet.
There are multiple different types of HandlerAdapter implementation classes in the Spring framework, each of which is adapted to a specific type of processor.
The HandlerAdapter interface defines the methods that the adapter must implement, including determining whether it supports processors, executing processors, and so on.
4. The core idea of the Adapter pattern is to unify objects of different types into a standard invocation method through the adapter, so that the caller does not need to care about the specific types of objects.
In the Spring framework, HandlerAdapters adapt different types of processors to the calling method of DispatcherServlet, enabling unified processing of different types of processors in DispatcherServlet.