springmvc source Interpretation (Introduction)

 

1, the client requests submitted DispatcherServlet
2, the controller DispatcherServlet the HandlerMapping one or more queries, found Controller processing request.
3, DispatcherServlet forwards the request to the Controller.
After 4, Controller call the business logic layer processing returns ModelAndView
5, DispatcherServlet query one or more ViewResoler view resolver, find the specified view ModelAndView
6, the view is responsible for displaying the results to the client

 

Source Description: First, we find a way to get the handler's start with DispatcherServlet

 

 

 

Then into this method, which can eventually return Handler or null, returns HandlerExecutionChain type (type of handler is not returned, but a chain of execution)

  First, determine whether handlerMappings not empty, if established, would enter the while loop, then handlerMappings is what is it? We look to open DeBug

 

So it should be added breakpoint Where? We should doDispatch at the core of the method () plus here.

 

 

Then go get Handler method of entering getHandler () method

As can be seen, is a collection HandlerMappings, there is also a set of two objects, two objects are to be matched with the handler

 

Enter the next step, we can see the mapping: BeanNameUrlHandlerMapping, in the collection we can see there is a BeanNameUrlHandlerMapping object, and then enter the if statement, handler is not empty, out of the loop, return handler

 

The return handler, the method getHandler

 

 

说了这么多,那么handlerMappings这个集合里面的两个对象到底是什么呢?我们再走下一步,进入 if 语句,我们看输出台上的mappingHandler,里面存了一个对象,内容里面有一个我们自己写的控制器,和一个拦截器

它将我们的控制器和拦截器封装成一个HandlerExecutionChain , 拦截器是spring系统自己定义的,每次执行的时候都会自带一个拦截器。

找到handler后,返回到中央控制器DispatcherServlet,中央控制器又拿着handler去请求handlerAdapter处理适配器,为什么需要适配器呢?我们带着这个疑问来看一下,跟获取handler的方法一样,找到获取适配器的方法

进入方法,这个方法藏得比较深,我们要进入多次

继续进入该方法

再进入该方法

到了,该方法返回的类型是HandlerAdapter,这个方法跟getHandler方法差不多一样,也有一个handlerAdapter集合,集合里有3个适配器,打个比方,这三个适配器就相当于笔记本的电源充电线,不同的笔记本需要使用不同的适配器,不同的handler就要使用不同的handlerAdapter,通过循环,找到匹配的适配器。为什么一上来就有这三个适配器呢?其实这三个是系统提前定义好的

在我们的jar包中可以找到,前端控制器的配置文件,我们来进入配置文件,可以看到我们之前看到的handler和handlerAdapter中,两个集合的对象。

这样我们就知道为什么我们一进去就已经有东西了,原来是在这里提前加载了。

逻辑思维:

说直白一点,首先拿到handler的目的是为了拿到控制器,比如在我们的请求路径是 localhost:8080/test ,通过获得handler,返回我们定义的控制器和一个拦截器,找到我们写的代码,那么Adapter就是找出与让控制器执行的方法。

那么为什么要搞这么麻烦呢?是为了解耦、提高程序的灵活性。

 

拿到适配器之后,接下来我们继续走,途中经过方法请求方式的判断

As to this point, we go look applyPreHandler method

This method is executed before our handler, to get the array of interceptor, the interceptor executed one by one

After completion of interceptor open, begin handler, eventually returned mv, mv is our ModelAndView, we go in and see

Continue to go

Then go

To, it can be seen, the final execution handler method returns ModelAndView, to cast into handler Controller, so can we define the execution controller.

 

To sum up:

The first one is: from 2 handler in, find a suitable controller

BeanNameUrlHandlerMapping @ 833153a, XML configuration way controller

The controller RequestMappingHandlerMapping @ 3602f818 annotation mode

 

The second is: three from the Adapter to find suitable processor

HttpRequestHandlerAdapter @ 28dc72d7, configuration of SimpleControllerHandlerAdapter @ 6b712691, XML's Adapter, annotation

Adapter RequestMappingHandlerAdapter @ 4d763f47 annotation mode

 

 

 

 

 

 

 

 

 

 

1, the client requests submitted DispatcherServlet

Guess you like

Origin www.cnblogs.com/wdss/p/11210573.html