首頁  >  文章  >  Java  >  Springboot自動組裝之注入DispatcherServlet怎麼實現

Springboot自動組裝之注入DispatcherServlet怎麼實現

WBOY
WBOY轉載
2023-06-01 10:22:06815瀏覽

原理概述

Springboot向外界提供web服務,底層依賴了springframework中的web模組(包含但不限於spring mvc核心類別DispatcherServlet)來實作

Springboot自動組裝之注入DispatcherServlet怎麼實現

#那麼springboot在什麼時機向容器注入DispatcherServlet這個核心類別的呢
注入的流程還是遵循了自動組裝流程,在springboot框架裡預設提供了該自動組裝的支援

在jar套件裡的spring.factories檔案裡有個org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration設定

Springboot自動組裝之注入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";
	// ... 省略代码
	}

在DispatcherServletAutoConfiguration中有兩個內部類別(配置類別) DispatcherServletConfiguration、DispatcherServletRegistrationConfiguration和兩個對應的Condition類別DefaultDispatcherServletCondition、DispatcherServletRegistrationCondition

DispatcherServletConfiguration#pterConfi​​ration#Page#3PSerration#Ssy#Dsy#Fsy#F) (Confi活動」一個內部#3v3v1 月的一個標註的一個內部是標註的一個類別,ConfimerationConfimeration#Pirration#Pirration#3v1Sv3vSerulration#P時期的一個內部是標註的一個類別,ConfimerationConfimeration#Pirration#Pirration#3v1Sv3vSerulration#P時期)一個標註的一個圖,會被容器自動掃描到;其有兩種方法: 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):設定類別採用Lite模式
  • @Conditional(DefaultDispatcherServletCondition.class):根據DefaultDispatcherServletCondition類別回傳值計算是否要注入,主要邏輯是檢驗Spring容器中是否已經存在一個名字為"dispatcherServlet"的Dis#patcherServlet
  • Servlet

  • @ConditionalOnClass(ServletRegistration.class):容器中必須要有ServletRegistration類別
  • @EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class }):載入HttpProperties與WebMvcProperties
  • @B:注入MultipartResolver 解析類別

DispatcherServletConfigurationConfiguration

該類別注入的邏輯與DispatcherServletConfiguration 相同,只不過在類別上註記了@Import(DispatcherServletConfiguration.class)那麼這兩個類別定義就有了先後順序

@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;
		}
	}

其中,DispatcherServletRegistrationBean繼承自ServletRegistrationBean,主要為DispatcherServlet提供服務。 DispatcherServletRegistrationBean和DispatcherServlet都提供了註冊Servlet並公開DispatcherServletPath資訊的功能

以上是Springboot自動組裝之注入DispatcherServlet怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除