In SpringtMVC, configure the functions of <mvc: annotation-driven/> and <mvc: default-servlet-handler/> and parse the source code,
SpringtMVC is learned based on Spring4.X. During the learning process, we are advised to set the following two items in the XML configuration file:
I have never understood why, but I am willing. So it took a little time to debug the source code and wanted to know why it was necessary.
Demo code address:
Https://github.com/cyhbyw/springMVC_atguigu_TongGang
Project name:
SpringMVC_DebugSourceCode
Now start debugging.
==== PS: The image may not be very clear. You can right-click the image and choose to view it on the new tab (the effect is better) ===
Scenario 1: When there are two tags
1. The process of initializing HandlerMapping is as follows, and it contains RequestMappingHandlerMapping! As shown in.
2. The HandlerAdapter initialization process is as follows and contains RequestMappingHandlerAdapter! As shown in.
Case 2: These two labels are not available (Note: You need to comment out the content of these two labels during debugging)
1. The HandlerMapping initialization process is as follows, and includes DefaultAnnotationHandlerMapping. As shown in.
From the source code, we can see that it calls the getdefastrategstrategies () method of Line588. With these two tags, The Line570 method is called.
2. The HandlerAdapter initialization process is as follows and contains AnnotationMethodHandlerAdapter! As shown in.
From the source code, we can see that it calls the getdefastrategstrategies () method of Line626. When these two labels exist, the Line608 method is called.
The HandlerMapping and HandlerAdapter used by SpringtMVC are different when there are and none of these labels. Comparison:
|
When these two tags exist |
When these two labels are not available |
HandlerMapping |
BeanNameUrlHandlerMapping SimpleUrlHandlerMapping RequestMappingHandlerMapping |
BeanNameUrlHandlerMapping DefaultAnnotationHandlerMapping |
HandlerAdapter |
HttpRequestHandlerAdapter SimpleControllerHandlerAdapter RequestMappingHandlerAdapter |
HttpRequestHandlerAdapter SimpleControllerHandlerAdapter AnnotationMethodHandlerAdapter |
We can see from the table that:
1. For HandlerMapping, there is a SimpleUrlHandlerMapping more when there is a tag than when there is no tag. More importantly, update DefaultAnnotationHandlerMapping to RequestMappingHandlerMapping! From the source code, we can also see that the former has been abandoned and it is recommended to use the latter.
2. For HandlerAdapter, update AnnotationMethodHandlerAdapter to RequestMappingHandlerAdapter! Similarly, the former has been abandoned and is recommended for use.
I don't know if it is because of the above reasons that we suggest adding these two labels. However, in general, it is always bad to use obsolete classes. Therefore, even if there are no other reasons, follow the suggestions and add these two labels.
The other reasons for adding these two labels are as follows (not yet fully confirmed ):
1. In addition to automatically registering the above RequestMappingHandlerMapping and RequestMappingHandlerAdapter, it also automatically registers ExceptionHandlerExceptionResolver
2. Supports data format conversion using ConversionService
3. Supports data formatting using NumberFormatAnnotation and DateTimeFormat
4. Support the use of RequestBody and ResponseBody annotations
The next article will conduct source code debugging and analyze how SpringtMVC is implemented and achieve the above differences