springmvc5中有四个非抽象的HandlerAdapter:
supports()方法用来判断当前HandlerAdapter是否可以调用处理handler;
可以使用BeanNameUrlHandlerMapping将请求URL"/testServlet"和handler(Servlet1)相映射,使用SimpleServletHandlerAdapter调用处理成功:
SimpleControllerHandlerAdapter可以处理类型为Controller的handler,对handler的处理是调用Controller的handleRequest()方法。
看具体的配置和handler:
可以使用BeanNameUrlHandlerMapping将请求URL"/testController"和handler(Controller1)相映射,使用SimpleControllerHandlerAdapter调用处理成功:
HttpRequestHandlerAdapter可以处理类型为HttpRequestHandler的handler,对handler的处理是调用HttpRequestHandler的handleRequest()方法。
可以使用BeanNameUrlHandlerMapping将请求URL"/testHttpRequestHandler"和handler(HttpRequestHandler1)相映射,使用HttpRequestHandlerAdapter调用处理成功:
RequestMappingHandlerAdapter可以处理类型为HandlerMethod的handler,对handler的处理是调用通过java反射调用HandlerMethod的方法。
只有RequestMappingHandlerAdapter有order属性,其他三个HandlerAdapter没有order属性。前三个HandlerAdapter处理的handler类型各不相同,处理也比较简单,不需要使用order区分优先级。
到这里,springmvc的四个HandlerAdapter就分析完了。前三个HandlerAdapter对handler的处理比较简单,HttpRequestHandlerAdapter和SimpleControllerHandlerAdapter直接调用固定唯一的方法handleRequest();
1.SimpleServletHandlerAdapter
2.SimpleControllerHandlerAdapter
3.HttpRequestHandlerAdapter
4.RequestMappingHandlerAdapter
springmvc通过HandlerMapping将请求URL映射到handler,再将handler交给HandlerAdapter进行调用处理。
HandlerAdapter里重要的有两个方法:
![](https://img-blog.csdnimg.cn/20200328181253995.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200328181343780.jpg)
supports()方法用来判断当前HandlerAdapter是否可以调用处理handler;
handle()方法用来调用handler,具体的来处理请求。
接下来对四个HandlerAdapter进行一一分析。
1.SimpleServletHandlerAdapter
![](https://img-blog.csdnimg.cn/20200328181409644.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
SimpleServletHandlerAdapter可以处理类型为Servlet的handler,对handler的处理是调用Servlet的service方法处理请求。
看具体的配置和handler:
![](https://img-blog.csdnimg.cn/20200328181457977.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
可以使用BeanNameUrlHandlerMapping将请求URL"/testServlet"和handler(Servlet1)相映射,使用SimpleServletHandlerAdapter调用处理成功:
![](https://img-blog.csdnimg.cn/20200328181555985.jpg)
2.SimpleControllerHandlerAdapter
![](https://img-blog.csdnimg.cn/20200328181649800.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
SimpleControllerHandlerAdapter可以处理类型为Controller的handler,对handler的处理是调用Controller的handleRequest()方法。
Controller是springmvc的一个接口:
![](https://img-blog.csdnimg.cn/20200328181959524.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
看具体的配置和handler:
![](https://img-blog.csdnimg.cn/2020032818202463.jpg)
![](https://img-blog.csdnimg.cn/20200328183340371.jpg)
可以使用BeanNameUrlHandlerMapping将请求URL"/testController"和handler(Controller1)相映射,使用SimpleControllerHandlerAdapter调用处理成功:
![](https://img-blog.csdnimg.cn/20200328182044358.jpg)
3.HttpRequestHandlerAdapter
![](https://img-blog.csdnimg.cn/2020032818211988.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
HttpRequestHandlerAdapter可以处理类型为HttpRequestHandler的handler,对handler的处理是调用HttpRequestHandler的handleRequest()方法。
HttpRequestHandler是springmvc的一个接口:
![](https://img-blog.csdnimg.cn/20200328182136361.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
看具体的配置和handler:
![](https://img-blog.csdnimg.cn/2020032818215796.jpg)
![](https://img-blog.csdnimg.cn/20200328182237677.jpg)
可以使用BeanNameUrlHandlerMapping将请求URL"/testHttpRequestHandler"和handler(HttpRequestHandler1)相映射,使用HttpRequestHandlerAdapter调用处理成功:
![](https://img-blog.csdnimg.cn/20200328182300480.jpg)
4.RequestMappingHandlerAdapter
RequestMappingHandlerAdapter类代码相比之前三个HandlerAdapter,代码要复杂的多,这里只截取重要的部分:
![](https://img-blog.csdnimg.cn/20200328182332936.jpg)
![](https://img-blog.csdnimg.cn/20200328182353100.jpg)
![](https://img-blog.csdnimg.cn/20200328182411226.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
RequestMappingHandlerAdapter可以处理类型为HandlerMethod的handler,对handler的处理是调用通过java反射调用HandlerMethod的方法。
看具体的配置和handler:
![](https://img-blog.csdnimg.cn/20200328182438621.jpg)
![](https://img-blog.csdnimg.cn/20200328182608870.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyMTkwODc=,size_16,color_FFFFFF,t_70)
只有RequestMappingHandlerAdapter有order属性,其他三个HandlerAdapter没有order属性。前三个HandlerAdapter处理的handler类型各不相同,处理也比较简单,不需要使用order区分优先级。
使用RequestMappingHandlerMapping将请求URL"/test11"和handler(TestController.test2())相映射(需在方法上添加@RequestMapping注解),使用RequestMappingHandlerAdapter调用处理成功:
![](https://img-blog.csdnimg.cn/20200328182629938.jpg)
到这里,springmvc的四个HandlerAdapter就分析完了。前三个HandlerAdapter对handler的处理比较简单,HttpRequestHandlerAdapter和SimpleControllerHandlerAdapter直接调用固定唯一的方法handleRequest();
SimpleServletHandlerAdapter直接调用固定唯一的方法service()。
RequestMappingHandlerAdapter实现起来就很复杂了,处理调用的方法不唯一也不固定,需要有更多的处理。
在HandlerAdapter处理的返回值上,虽然统一的返回ModelAndView对象,但HttpRequestHandlerAdapter和SimpleServletHandlerAdapter实际上都是直接returnnull;
SimpleControllerHandlerAdapter返回handler(Controller)处理的结果,ModelAndView对象或者null;
RequestMappingHandlerAdapter是通过反射调用获取的结果,这个结果可能是ModelAndView对象,也可能是String类型对象,还可以是一个map等,需要对返回的结果做更多的判断处理,再返回统一的ModelAndView对象。
后面会对RequestMappingHandlerAdapter做更详细的分析。