Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the handlerMappings object in SpringMVC

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use handlerMappings objects in SpringMVC. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

From the example used in SpringMVC source code parsing, a http://localhost:9090/web/hi?name=yang request is called to the following place, and a special object handlerMappings is found. When you ask uri to find handler, you need to find it through this handlerMapping. What about handlerMappings?

From the breakpoint debugging, we can see that there are a total of five handlerMappings, namely WebMvcConfigurationSupportXXX,RequestMappingHandlerMapping and BeanNameUrlHandlerMapping.

First of all, let's find out what this handlerMappings is. Officially, handlerMappings is a Bean type within DispatcherServlet, and there are many Bean types within DispatcherServlet, including HandlerMapping,HandlerAdapter,HandlerExceptionResolver,ViewResolver,LocaleResolver, ThemesResolver,Multipart Resolver and FlashMapManager.

Where HandlerMapping is a handler that maps requests to a list of interceptors for preprocessing and post-processing, except for WebMvcConfigurationSupportXXX's custom mapping processor.

There are two main built-in types:

(1) RequestMappingHandlerMapping (it supports @ RequestMapping modification)

(2) SimpleUrlHandlerMapping (which maintains explicit registration of the URI path mode for the processor)

I. what are the elements inside the handlerMappings collection and what is the purpose?

According to the official documentation, there are two main mapping processor types, RequestMappingHandlerMapping and SimpleUrlHandlerMapping, within the handlerMappings collection, which are used to handle different types of Controller.

We know that you can declare a Controller by annotating @ Controller or @ RestController, and then decorate the class name or method name with @ RequestMapping. In this way, the RequestMappingHandlerMapping mapping processor maps the request address and method name. So what kind of Controller is SimpleUrlHandlerMapping used to deal with, that is, classes that implement the Controller interface or the HttpRequestHandler interface? this way of declaring Controller is that the early practice is now less used in actual development, but it is also supported by SpringMVC.

For RequestMappingHandlerMapping

When we decorate the method with @ RequestMapping+ request address, this type of handlerMapping will be parsed and mapped by such a mapper, that is, the RequestMappingHandlerMapping type mapper is specially designed for annotation declaration mapping, and the mapping relationship will be stored in the Map of this handlerMapping.

For SimpleUrlHandlerMapping

This mapper parses and maps classes that inherit the Controller class or implement the HttpRequestHandler interface.

To verify with a code example, we create two new classes to implement the Controller interface and the HttpRequestHandler interface, respectively, and write the request receiving method in the class. Then the Controller declared through these two kinds of Controller through the request address will be stored and mapped by SimpleUrlHandlerMapping when querying the request method, and the mapping relationship will be stored in the Map of this SimpleUrlHandlerMapping.

(1) how to implement the Controller API:

(2) how to implement the HttpRequestHandler interface:

Then redo the debug, and use the http://localhost:9090/beanweb or http://localhost:9090/beanweb2 request, and you will see that the RequestMappingHandlerMapping of the first annotation type will be skipped when looking for handler in handlerMapping, and the real parsing is the second non-annotated method, SimpleUrlHandlerMapping:

So we now understand what the data in the handlerMappings mapping processor collection looks like and what kind of requests are handled by the mapping processors stored in the collection. So how is this collection data initialized?

II. How is the handlerMappings collection initialized?

Through the Find Usages tool of IDEAJ, you can search for calls in the this.handlerMappings in figure 1 of this article, and you can find the following call chain layer by layer:

HttpServletBean.init-- > initServletBean ()-> FrameworkServlet.initServletBean-- >

FrameworkServlet.initWebApplicationContext-- > FrameworkServlet.onRefresh

-- > DispatcherServlet.onRefresh-- > DispatcherServlet.initStrategies-- > DispatcherServlet.initHandlerMappings

-> DispatcherServlet.getDefaultStrategies-- > strategies.add ((T) strategy)

From the previous study of the first two processes of SpringMVC, DispatcherServlet inherits FrameworkServlet and integrates HttpServlet at the same time, that is to say, DispatcherServlet is essentially a Servlet, so if the container loads this Servlet and initializes it immediately when loading, its init method will be automatically executed when loading, so there is the first HttpServletBean.init entry of the above call chain.

Our goal is to find out where the core operation of handlerMappings is performed, and through the call relationship we find the core code in DispatcherServlet.initHandlerMappings:

From the objects returned by the above code, we can boldly infer that matchingBeans is the handlerMappings collection we want to know, and this method initializes various types of HandlerMapping internally. By looking at the values of the two mapper objects that we focus on, we can see the relationship:

Above you can see the Map collection and handlerMap collection of urlLookup that we mentioned earlier. To understand how these different request addresses are assigned to different types of mappers, let's move on to this core approach:

This is actually done by the following line of code:

Lbf.getBeansOfType (type, includeNonSingletons, allowEagerInit)

And the real logic worth thinking about is

String [] beanNames = getBeanNamesForType (type, includeNonSingletons, allowEagerInit)

The logic of the above method automatically generates a handlerMappings with five types of handlerMapping, so how do these five mappers come into being?

We can know from the core code that by passing in a type of type= "HandlerMapping", matching all the BeanName of type HandlerMapping in the collection of beanDefinitionNames, then putting it into the result array to return, and then getting the Bean of the corresponding HandlerMapping type in the singleton pool based on BeanName.

That is to say, these HandlerMapping mapping processors are generated by matching in BeanDefinition, and if the matching gets the corresponding type of Bean from the singleton pool, so there are these five types in the final handlerMappings. As for how the request path and method name are bound to the corresponding mapper as KQuery V, as the personal ability to read the source code is limited, the editor has not yet understood this part of the code, as an unsolved mystery of the editor.

Of course, since five types of mappers are generated through BD, when did these BeanDefinition be added to the Spring container?

You have to mention the configuration file for the HandlerMapping mapping processor: the DispatchServlet.properties file, which defines which component declarations are used in the built-in.

In fact, through the debugging of the above source code, the process for HandlerAdaper is actually very similar to HandlerMapping.

Summary

(1) there are 5 different types of mappers in the handlerMappings mapper collection.

The Controller,SimpleUrlHandlerMapping used by the WebMvcConfigurationSupportXXX,RequestMappingHandlerMapping and BeanNameUrlHandlerMapping,RequestMappingHandlerMapping mapping processors to request addresses and method names to map using annotations is used to implement the Controller interface or the Controller that implements the HttpRequestHandler interface

(2) handlerMappings mapper set initialization goes through a call chain

Initialization starts automatically when DispatchServlet starts

HttpServletBean.init-- > initServletBean ()-> FrameworkServlet.initServletBean-- > FrameworkServlet.initWebApplicationContext-- > FrameworkServlet.onRefresh-> DispatcherServlet.onRefresh-- > DispatcherServlet.initStrategies-- > DispatcherServlet.initHandlerMappings-> DispatcherServlet.getDefaultStrategies-- > strategies.add ((T) strategy)

The result of initialization is a collection of handlerMappings mappers containing five different types of mappers, each of which has a mapping relationship maintained by Map

(3) each mapping processor of the handlerMappings mapper set

BeanDefinition is generated at initialization, matching all mapping processors in all BeanDefinitionNames collections through BeanDefinitionName and the type=HandlerMapping type passed in, and then getting their corresponding Bean instances from the singleton pool and putting them into the handlerMappings

(4) the initialization of HandlerAdaper is similar to that of HandlerMapping.

(5) the mapping relation of

This part of the code editor is not yet aware of where it was generated and maintained in the mapping processor.

What is the extension of handlerMappings?

We know that static resources under WEB-INF cannot be accessed directly through url in SpringMVC. For example, we can create a new 1.jsp file in the project webapp/jsp/ through the

The following 404 errors are reported when localhost:9090/jsp/1.jsp accesses

But SpringBoot can access static resources under packages such as webapp,static,resource directly through url. Why is this so? The reason is that SpringBoot has expanded SpringMVC's handlerMappings.

The reason why SpringMVC cannot access static resources is that none of the default mappers in handlerMappings can parse such requests for access to static resources, so naturally such requests are ignored.

For SpringBoot, it has implemented a mapping processor for this kind of access to static resources and handed it over to handlerMappings to manage, so it can achieve a similar extension. For this knowledge point, when you learn about SpringBoot, you can see if you can study this special mapping processor. Here you can temporarily know such a conclusion.

Thank you for reading! This is the end of this article on "how to use handlerMappings objects in SpringMVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report