In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the source code analysis of the HandlerAdapter adapter pattern, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
What is an adapter pattern for HandlerAdapter adapters in SpringMVC
Definition: convert the interface of one system into another form, so that the interface that cannot be called directly becomes callable.
Adapter pattern application scenario
Integration of multiple logging frameworks in Mybatis
SpringMVC adapter mode
Compatibility between new and old versions
Source Code Analysis of SpringMVC Adapter pattern
1. Find the specific request method through URL
MappedHandler = this.getHandler (processedRequest)
Initialize three adapters here
Go first, parent class.
Return to true
Protected boolean supportsInternal (HandlerMethod handlerMethod) {return true;}
Get the corresponding adapter
The HandlerAdapter interface takes a look at all adapter types
Take a look at these adapters:
AbstractHandlerMethodAdapter implements HandlerAdapterpublic final boolean supports (Object handler) {return handler instanceof HandlerMethod & & this.supportsInternal ((HandlerMethod) handler);} HttpRequestHandlerAdapter implements HandlerAdapterpublic boolean supports (Object handler) {return handler instanceof HttpRequestHandler;} RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapterprotected boolean supportsInternal (HandlerMethod handlerMethod) {return true;} SimpleControllerHandlerAdapter implements HandlerAdapterpublic boolean supports (Object handler) {return handler instanceof Controller;} SimpleServletHandlerAdapter implements HandlerAdapterpublic boolean supports (Object handler) {return handler instanceof Servlet;}
Inherit the adapter used in the Controller way: SimpleControllerHandlerAdapter
HTTP request processor Adapter: HttpRequestHandlerAdapter
Processor adapter for annotated mode (@ Controller): RequestMappingHandlerAdapter
If you don't use an adapter
If (hanlder instanceof Controller) {/ / execute Controller Adapter} If (hanlder instanceof HttpControler) {/ / execute our HttpController} If (hanlder instanceof ServletControler) {/ / execute our HttpController} If (hanlder instanceof AnnotationControler) {/ / execute our AnnotationController}
Simple implementation of interface
@ Controller ("/ httpRequestHandler") public class ExtHttpRequestHandlerAdapter implements HttpRequestHandler {@ Override public void handleRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println ("httpRequestHandler");}}
At this time, the HttpRequestHandlerAdapter adapter will be executed.
Analog SpringMVC Adapter pattern
HandlerAdapter
Public interface HandlerAdapter {/ * determine which HandlerAdapter type is based on hanlder. If the corresponding type is found, return true * / boolean supports (Object handler); / * * execute our request method * / void handle (Object handler);}
HandlerAdapter subclass
Adapter in public class AnnotationHandlerAdapter implements HandlerAdapter {/ * annotated form * / public boolean supports (Object handler) {return (handler instanceof AnnotationController);} public void handle (Object handler) {((AnnotationController) handler) .hanlder ();}} public class HttpRequestHandlerAdapter implements HandlerAdapter {/ * Http type adapter * / public boolean supports (Object handler) {return (handler instanceof HttpController) } public void handle (Object handler) {(HttpController) handler) .hanlder ();}}
Controller
Public interface Controller {/ / request method void hanlder ();}
Controller subclass
Public class AnnotationController implements Controller {public void hanlder () {System.out.println ("AnnotationController");}} public class HttpController implements Controller {public void hanlder () {System.out.println ("HttpController");}}
DispatcherServlet
Public class DispatcherServlet {private List handlerAdapters; public DispatcherServlet () {handlerAdapters = new ArrayList (); handlerAdapters.add (new HttpRequestHandlerAdapter ()); handlerAdapters.add (new AnnotationHandlerAdapter ());} public void dispatcher () {/ / 1. HanlderAnnotationController hanlder = new AnnotationController (); / / 2 has been obtained. Get the specific adapter HandlerAdapter handlerAdapter = getHandlerAdapter (hanlder); / / 3. Execute our request scheme handlerAdapter.handle (hanlder);} public HandlerAdapter getHandlerAdapter (Controller controller) {if (this.handlerAdapters! = null) {for (HandlerAdapter ha: this.handlerAdapters) {if (ha.supports (controller)) {return ha;} return null } public static void main (String [] args) {new DispatcherServlet (). Dispatcher ();}} the above is the source code analysis of the HandlerAdapter adapter pattern. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.