您好,登錄后才能下訂單哦!
這篇文章主要介紹“Springboot自動裝配之注入DispatcherServlet怎么實現(xiàn)”,在日常操作中,相信很多人在Springboot自動裝配之注入DispatcherServlet怎么實現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Springboot自動裝配之注入DispatcherServlet怎么實現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
Springboot向外界提供web服務(wù),底層依賴了springframework中的web模塊(包含但不限于spring mvc核心類DispatcherServlet)來實現(xiàn)
那么springboot在什么時機向容器注入DispatcherServlet這個核心類的呢
注入的流程還是遵循了自動裝配流程,在springboot框架里默認提供了該自動裝配的支持
在jar包里的spring.factories文件里有個 org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration配置
@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中有兩個內(nèi)部類(配置類) DispatcherServletConfiguration、DispatcherServletRegistrationConfiguration和兩個對應(yīng)的Condition類DefaultDispatcherServletCondition、DispatcherServletRegistrationCondition
DispatcherServletConfiguration是DispatcherServletAutoConfiguration 的一個內(nèi)部類,并被 @Configuration標注,會被容器自動掃描到;其有兩個方法: 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):根據(jù)DefaultDispatcherServletCondition類返回值計算是否要注入,主要邏輯是檢驗Spring容器中是否已經(jīng)存在一個名字為"dispatcherServlet"的DispatcherServlet
@ConditionalOnClass(ServletRegistration.class):容器中必須要有ServletRegistration類
@EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class }):加載HttpProperties和WebMvcProperties
@B:注入 MultipartResolver 解析類
該類注入的邏輯與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()); // 設(shè)置名稱 registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup()); multipartConfig.ifAvailable(registration::setMultipartConfig); return registration; } }
其中,DispatcherServletRegistrationBean繼承自ServletRegistrationBean,主要為DispatcherServlet提供服務(wù)。DispatcherServletRegistrationBean和DispatcherServlet都提供了注冊Servlet并公開DispatcherServletPath信息的功能
到此,關(guān)于“Springboot自動裝配之注入DispatcherServlet怎么實現(xiàn)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。