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

Source: Internet
Author: User

Spring mvc configuration <mvc: annotation-driven/> and <mvc: default-servlet-handler/> source code parsing,

As mentioned in the previous article, HandlerMapping and HandlerAdapter used at the bottom layer of SpringtMVC are different when there are and none of these labels. Now let's debug the source code to uncover the true underlying implementation of SpringtMVC.

Demo code address:

Https://github.com/cyhbyw/springMVC_atguigu_TongGang

Project name:

SpringMVC_DebugSourceCode

 

Now start debugging.

==================>>>>
PS: The image may not be clear. You can right-click the image and choose to view it on the new tab.
Alternatively, you can right-click the image, select "Save image as", save it locally, and set the number (we recommend that you use the format 01, 02, 03 ...... To number)
Alternatively, you can right-click the image, select "copy image", save the image to your local device, and compile the number (we recommend that you use the numbers 01, 02, 03 ...... To number)
You can select one of the above three methods to obtain and view clearer images ~~
<====================

Let's take a look at SpringMVC's behavior without the two tags. Tip: You need to comment out the content of these two tags in the XML configuration file!

01. the reader of the Bean definition document (actually reading the content in the XML configuration file). SpringMVC reads the component-scan element and calls the Line166 method. Pay attention to the thread stack information, this method is located in the refresh () method call path of Line501 of AbstractApplicationContext (refresh () method is the core method of SpringIOC container management, which will be mentioned later)

 

02. parse the element; call the Line1339 method first, and then call the Line1343 method; the Line1345 resolver () method is very important (for details later)

 

03. Now enter the resolve () method; then call the Line128 init () method

 

 

04. The init () method registers a batch of Bean definition parsers. For example, Line35 defines the parsers of the component-scan element.

 

 

05. The registration method is to put it into the map of parsers (put it here and it will be used later)

 

 

06. Return to the previous layer. After the resolve () method is processed, call the handler. parse () method.

 

 

07. The parse () method will first find the parser of the corresponding element

 

 

08. The essence of searching is to retrieve the value of the corresponding element from the map just now.

 

 

09. After finding the parser, continue to call the parse () method.

 

 

10. continue calling

 

11. continue calling

 

 

12. beanDefs of the row where the breakpoint is located in the following two images. add (the registerPostProcessor () method of the registerPostProcessor method will be called in sequence (understandable, or equivalent to for loop call, but SpringtMVC has other judgment logic)

 

13. Continue to call the registerBeanDefinition () method.

 

14. Add beanName to beanDefinitionNames through Line851 for later use

 

15. After multiple calls, we can see that the number of elements in beanDefinitionNames is increasing.

 

16. the component-scan element defined in the XML configuration file has been processed, and now the bean element is processed (this is actually the InternalResourceViewResolver defined in the XML file, it only does not assign a value for its id/name, so it is displayed as "bean: null ")

 

 

17. Similarly, InternalResourceViewResolver will be added to beanDefinitionNames.

 

18. So far, beanDefinitionNames has saved eight elements. It is time to think about when beanDefinitionNames will be used.

As mentioned above, AbstractApplicationContext has a refresh () method. This method is the core method of SpringIOC container management, which includes several other important method calls.

The refresh () method is very important !!!!

The refresh () method is very important !!!!

The refresh () method is very important !!!!

All the call methods in steps 1-17 above are actually in the obtainFreshBeanFactory () method of line501. Now it is time to call other methods.

After debugging, SpringtMVC will go to the finishFresh () method of Line532.

 

 

19. now the initStrategies () method of DispatcherServlet is reached. Note that there are many stack paths in view, so we will not detail them anymore. At the same time, we can see that the initHandlerMappings (context) of Line482 is here) the initHandlerAdapters (context) method of & Line483 is actually getting HandlerMapping & HandlerAdapter from context --> beanFactory --> beanDefinitionNames

 

20. Sorry, I cannot get it (this is easy to understand, because the beanDefinitionNames we see above does not have any HandlerMapping or HandlerAdapter), so the code goes to Line588 to get the default value.

 

21. The default value is obtained from defaultStrategies.

 

 

22. defaultStrategies uses static {} to read the content in the DispatcherServlet. properties file.

 

23. The file content is as follows; two HandlerMapping and three handleradapters are used by default (this is the default value used by SpringtMVC when the two labels are not configured)

 

 

24. The logic for initializing HandlerAdapter is similar.

 

 

In the preceding 24 steps, the SpringtMVC processing process is not added. Next, we will analyze the processing process for these two tags.

In general, the difference is that different instances are created in the middle, and the method of no instance is called to obtain different beans.

Let's take a look. The same logic is not described in detail. The following description and diagram are mainly reflected in some different points.

01. Now start processing the annotation-driven Element

 

02. Get a different NamespaceHandler and call its init () method.

 

 

03. The registration content is certainly different. Obviously, the first one is annotation-drivern and the corresponding class is AnnotationDrivenBeanDefinitionParser.

 

 

04. Of course, the registered AnnotationDrivenBeanDefinitionParser is obtained.

 

 

05. The parse () method is naturally different. Note that Line204 will register HANDLER_MAPPING_BEAN_NAME

 

 

06. The HANDLER_MAPPING_BEAN_NAME is actually RequestMappingHandlerMapping.

 

 

07. You can see that RequestMappingHandlerMapping has been successfully added to beanDefinitionNames (then, you can retrieve it later)

 

 

08. Register HANDLER_ADAPTER_BEAN_NAME

 

09. HANDLER_ADAPTER_BEAN_NAME is actually RequestMappingHandlerAdapter

 

 

10. Add RequestMappingHandlerAdapter to beanDefinitionNames (which can be used later ~~)

 

 

 

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.