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 realize the injection DispatcherServlet of Springboot automatic Assembly

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize the injection DispatcherServlet of Springboot automatic assembly". In the daily operation, I believe that many people have doubts about how to realize the injection DispatcherServlet of Springboot automatic assembly. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the injection DispatcherServlet of Springboot automatic assembly". Next, please follow the editor to study!

Overview of the principle

Springboot provides web services to the outside world, and the underlying layer relies on the web module in springframework (including but not limited to the spring mvc core class DispatcherServlet) to implement

So when did springboot inject the core class DispatcherServlet into the container?

The injection process still follows the automatic assembly process, which is supported by default in the springboot framework.

There is an org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration configuration in the spring.factories file in the jar package

@ AutoConfigureOrder (Ordered.HIGHEST_PRECEDENCE) @ Configuration (proxyBeanMethods = false) @ ConditionalOnWebApplication (type = Type.SERVLET) @ ConditionalOnClass (DispatcherServlet.class) @ AutoConfigureAfter (ServletWebServerFactoryAutoConfiguration.class) public class DispatcherServletAutoConfiguration {/ * The bean name for a DispatcherServlet that will be mapped to the root URL "/" * / public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet" / * The bean name for a ServletRegistrationBean for the DispatcherServlet "/" * public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration"; / /. Omit the code}

There are two inner classes (configuration class) DispatcherServletConfiguration, DispatcherServletRegistrationConfiguration and two corresponding Condition classes DefaultDispatcherServletCondition and DispatcherServletRegistrationCondition in DispatcherServletAutoConfiguration

DispatcherServletConfiguration

DispatcherServletConfiguration is an inner class of DispatcherServletAutoConfiguration, marked with @ Configuration and automatically scanned by the container. It has two methods: 1, dispatcherServlet;2, multipartResolver

Configuration (proxyBeanMethods = false) @ Conditional (DefaultDispatcherServletCondition.class) @ ConditionalOnClass (ServletRegistration.class) @ EnableConfigurationProperties ({HttpProperties.class, WebMvcProperties.class}) protected static class DispatcherServletConfiguration {/ / @ A @ Bean (name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet (HttpProperties httpProperties, WebMvcProperties webMvcProperties) {DispatcherServlet dispatcherServlet = new DispatcherServlet () DispatcherServlet.setDispatchOptionsRequest (webMvcProperties.isDispatchOptionsRequest ()); dispatcherServlet.setDispatchTraceRequest (webMvcProperties.isDispatchTraceRequest ()); dispatcherServlet.setThrowExceptionIfNoHandlerFound (webMvcProperties.isThrowExceptionIfNoHandlerFound ()); dispatcherServlet.setPublishEvents (webMvcProperties.isPublishRequestHandledEvents ()); dispatcherServlet.setEnableLoggingRequestDetails (httpProperties.isLogRequestDetails ()); return dispatcherServlet } / / @ B @ Bean @ ConditionalOnBean (MultipartResolver.class) @ ConditionalOnMissingBean (name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) public MultipartResolver multipartResolver (MultipartResolver resolver) {/ / Detect if the user has created a MultipartResolver but named it incorrectly return resolver }}

@ A:

@ Configuration (proxyBeanMethods = false): the configuration class uses Lite mode

@ Conditional (DefaultDispatcherServletCondition.class): calculates whether to inject based on the return value of the DefaultDispatcherServletCondition class. The main logic is to verify whether a DispatcherServlet named "dispatcherServlet" already exists in the Spring container.

@ ConditionalOnClass (ServletRegistration.class): must have a ServletRegistration class in the container

@ EnableConfigurationProperties ({HttpProperties.class, WebMvcProperties.class}): load HttpProperties and WebMvcProperties

@ B: injecting MultipartResolver parsing class

DispatcherServletRegistrationConfiguration

The logic of this class injection is the same as that of DispatcherServletConfiguration, except that @ Import (DispatcherServletConfiguration.class) is annotated on the class, so the two class definitions are in order.

@ Configuration (proxyBeanMethods = false) @ Conditional (DispatcherServletRegistrationCondition.class) @ ConditionalOnClass (ServletRegistration.class) @ EnableConfigurationProperties (WebMvcProperties.class) @ Import (DispatcherServletConfiguration.class) / / @ A protected static class DispatcherServletRegistrationConfiguration {@ Bean (name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ ConditionalOnBean (value = DispatcherServlet.class Name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServletRegistrationBean dispatcherServletRegistration (DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider multipartConfig) {DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean (dispatcherServlet, webMvcProperties.getServlet (). GetPath ()) / / set the name registration.setName (DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup (webMvcProperties.getServlet () .getLoadOnStartup ()); multipartConfig.ifAvailable (registration::setMultipartConfig); return registration;}}

Among them, DispatcherServletRegistrationBean inherits from ServletRegistrationBean and mainly provides services for DispatcherServlet. Both DispatcherServletRegistrationBean and DispatcherServlet provide the ability to register Servlet and expose DispatcherServletPath information

At this point, the study on "how to implement the injection of DispatcherServlet for Springboot automatic assembly" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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