Home > Java > javaTutorial > How to implement Springboot automatic assembly by injecting DispatcherServlet

How to implement Springboot automatic assembly by injecting DispatcherServlet

WBOY
Release: 2023-06-01 10:22:06
forward
822 people have browsed it

Principle Overview

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

How to implement Springboot automatic assembly by injecting DispatcherServlet

So when does springboot inject the core class DispatcherServlet into the container?
The injection process still follows the automatic assembly process, and the support for automatic assembly is provided by default in the springboot framework.

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

How to implement Springboot automatic assembly by injecting DispatcherServlet

@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";
	// ... 省略代码
	}
Copy after login

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

DispatcherServletConfiguration

DispatcherServletConfiguration is an internal class of DispatcherServletAutoConfiguration and is annotated with @Configuration and will be 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;
		}
	}
Copy after login

@A:

  • @Configuration(proxyBeanMethods = false): The configuration class adopts Lite mode

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

  • @ConditionalOnClass(ServletRegistration.class): There must be a ServletRegistration class in the container

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

@B: Inject MultipartResolver parsing class

DispatcherServletRegistrationConfiguration

The injected logic of this class is the same as DispatcherServletConfiguration, but it is annotated on the class @Import(DispatcherServletConfiguration.class), then the two class definitions have a sequence

@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<MultipartConfigElement> multipartConfig) {
			DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
					webMvcProperties.getServlet().getPath());
			// 设置名称		
			registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
			registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
			multipartConfig.ifAvailable(registration::setMultipartConfig);
			return registration;
		}
	}
Copy after login

Among them, DispatcherServletRegistrationBean inherits from ServletRegistrationBean and mainly provides services for DispatcherServlet. Both DispatcherServletRegistrationBean and DispatcherServlet provide the function of registering Servlet and exposing DispatcherServletPath information

The above is the detailed content of How to implement Springboot automatic assembly by injecting DispatcherServlet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template