丨新特性tomcat和jetty如何處理springboot應(yīng)用_第1頁
丨新特性tomcat和jetty如何處理springboot應(yīng)用_第2頁
丨新特性tomcat和jetty如何處理springboot應(yīng)用_第3頁
丨新特性tomcat和jetty如何處理springboot應(yīng)用_第4頁
丨新特性tomcat和jetty如何處理springboot應(yīng)用_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

既然要支持多種Web容器,SpringBoot對內(nèi)嵌式WebpublicinterfaceWebServervoidstart()throwsvoidstop()throwsint5各種Web容器比如Tomcat和JettySpringBootServletWebServerFactoryWeb象就是上面提到的WebServer。publicinterfaceServletWebServerFactoryWebServergetWebServer(ServletContextInitializer...3可以看到publicinterfaceServletContextInitializervoidonStartup(ServletContextservletContext)throws3這里請注意,上面提到的getWebServer方調(diào)用ServletContextInitializer的onStartup方法,也就是說如果你想在Servlet容器啟動時做一些事情,比如你自己的Servlet,可以實現(xiàn)一個ServletContextInitializer,在Web容器啟動時,SpringBoot會ServletContextInitializeronStartup方法。為了支持對內(nèi)嵌式Web容器的定制化,SpringBoot還定義了BeanPostProcessorpostProcessBeforeInitializationSpring中WebServerFactoryCustomizerBean,publicinterfaceWebServerFactoryCustomizer<TextendsWebServerFactory>voidcustomize(T3Web鋪墊了這些接口,我們再來看看SpringBoot是如何實例化和啟動一個Web容器的。我們知道,Spring的是一個ApplicationContext 實現(xiàn)了著名的refresh方法,它用來新建或者刷新一個ApplicationContext,在refresh方法中會調(diào)用onRefresh方法, ApplicationContext的子類可以重寫這個方法onRefresh方法,來實現(xiàn)特定Context的刷新邏輯,因此ServletWebServerApplicationContext就是通過重寫onRefresh方法來創(chuàng)建內(nèi)嵌式的Web容器,具體創(chuàng)建過程是這樣的:protectedvoidonRefresh()34try5//重寫onRefresh方法,調(diào)用createWebServer創(chuàng)建和啟動67}8catch(Throwableex)9}}//createWebServerprivatevoidcreateWebServer()//這里WebServer是SpringBoot抽象出來的接口,具體實現(xiàn)類就是不同的WebWebServerwebServer=ServletContextservletContext=//如果Webif(webServer==null&&servletContext==null)//通過WebServletWebServerFactoryfactory=//注意傳入了一個this.webServer=factory.getWebServer(new}elseif(servletContext!=null)try}catch(ServletExceptionvar4)}}}getWebSeverTomcatTomcatAPIpublicWebServergetWebServer(ServletContextInitializer...initializers)//1.實例化一個Tomcat,可以理解為ServerTomcattomcat=new4//2.FilebaseDir=this.baseDirectory!=null?this.baseDirectory:8//3.Connectorconnector=new//4.創(chuàng)建定制版的"Context"this.prepareContext(tomcat.getHost(),return20prepareContextContextTomcatContext組件,為了方便控制Context組件的行為,SpringBoot定義了自己的TomcatEmbeddedContext,它擴展了Tomcat的StandardContext:1classTomcatEmbeddedContextextendsStandardContextServlet的三種方式Servlet在SpringBoot啟動類上加上 ponentScan注解后 使用@WebServlet、@WebFilter、@WebListener標(biāo)記的Servlet、Filter、Listener就可以自動到Servlet容器中,無需其他代碼 publicclass@WebServlet(/opublicclassoServletextendsHttpServlet在Web應(yīng)用的類上加 ponentScan,并且在Servlet類上加@WebServlet,這樣SpringBoot會負責(zé)將Servlet到內(nèi)嵌的Tomcat中同時SpringBoot也提供了ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean這三個類分別用來Servlet、Filter、Listener。假如要一個Servlet,可以這樣做:publicServletRegistrationBeanservletRegistrationBean()returnnewServletRegistrationBean(newoServlet(),"/4這段代碼實現(xiàn)的方法返回一個ServletRegistrationBean,并將它當(dāng)作Bean到Spring中,因此你需要把這段代碼放到SpringBoot自動掃描的中,或者放到@Configuration標(biāo)識的類中。Bean,SpringBootonStartuppublicclassMyServletRegisterimplementsServletContextInitializer345publicvoidonStartup(ServletContextservletContext)67//Servlet3.0規(guī)范新的8ServletRegistrationmyServlet=9.addServlet("oServlet",myServlet.setInitParameter("name","o}}onStartupServletContext,可以通過調(diào)用它的addServlet方法來動態(tài)新的Servlet,這是Servlet3.0以后才有的功能。WebSpringBoot中定制Web容器。在Spring2.0Web第式是通過通用的Web容器工廠ConfigurableServletWebServerFactory,來定制一些Web容器通用的參數(shù):publicclassMyGeneralCustomizerWebServerFactoryCustomizer<ConfigurableServletWebServerFactory>4 publicvoidcustomize(ConfigurableServletWebServerFactoryfactory)678}9第二種方式WebTomcatServletWebServerFactory定制。下面的例子里,我們給Tomcat增加一個Valve,這個Valve的功能是向請求頭里添加traceid,用于分布式追蹤。TraceValve的定義如下:classTraceValveextendsValveBasepublicvoidinvoke(Requestrequest,Responseresponse)throwsIOException,47Valvenext=if(null==next) 16

next.invoke(request,publicclassMyTomcatCustomizerWebServerFactoryCustomizer<TomcatServletWebServerFactory>4publicvoidcustomize(TomcatServletWebServerFactoryfactory)factory.addEngineValves(newTraceValve() 12今天我們學(xué)習(xí)了SpringBoot如何利用Web容器的API來啟動Web容器、如何向Web容器Servlet,以及如何定制化Web容器,除了給Web容器配置參數(shù),還可以增加或者修改Web容器本身的組件。我在文章中提到,通過ServletContextInitializer接口可以向Web容器Servlet,那ServletContextInitializer跟Tomcat中的ServletContainerInitializer有什么區(qū)別和聯(lián)系不知道今天的內(nèi)容你消化得如何?如果還有疑問,請大膽的在留言區(qū)提問,也歡迎你的課后思考和心得記錄下來,與我和其他同學(xué)一起討論。如果你覺得今天有所收獲,歡迎你把它給你的朋友。 售賣。頁面已增加防盜追蹤,將依 上一 27|新特性:Tomcat如何支持異步言言老師,springboot中g(shù)etWebServer方法的實現(xiàn)類不僅有tomcat,還有其

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論