SpringtMVC configuration & lt; mvc: annotation-driven/& gt; and & lt; mvc: default-servlet-handler/& gt; functions and source code parsing,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.