日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

SpringBoot內置tomcat啟動原理詳解

瀏覽:185日期:2023-03-13 15:56:41
前言

不得不說SpringBoot的開發者是在為大眾程序猿謀福利,把大家都慣成了懶漢,xml不配置了,連tomcat也懶的配置了,典型的一鍵啟動系統,那么tomcat在springboot是怎么啟動的呢?

內置tomcat

開發階段對我們來說使用內置的tomcat是非常夠用了,當然也可以使用jetty。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.6.RELEASE</version></dependency>

@SpringBootApplicationpublic class MySpringbootTomcatStarter{ public static void main(String[] args) {Long time=System.currentTimeMillis();SpringApplication.run(MySpringbootTomcatStarter.class);System.out.println('===應用啟動耗時:'+(System.currentTimeMillis()-time)+'==='); }}

這里是main函數入口,兩句代碼最耀眼,分別是SpringBootApplication注解和SpringApplication.run()方法。

發布生產

發布的時候,目前大多數的做法還是排除內置的tomcat,打瓦包(war)然后部署在生產的tomcat中,好吧,那打包的時候應該怎么處理?

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 --> <exclusions><exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId></exclusion> </exclusions></dependency><!--添加servlet-api依賴---><dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope></dependency>

更新main函數,主要是繼承SpringBootServletInitializer,并重寫configure()方法。

@SpringBootApplicationpublic class MySpringbootTomcatStarter extends SpringBootServletInitializer { public static void main(String[] args) {Long time=System.currentTimeMillis();SpringApplication.run(MySpringbootTomcatStarter.class);System.out.println('===應用啟動耗時:'+(System.currentTimeMillis()-time)+'==='); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(this.getClass()); }}從main函數說起

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class[]{primarySource}, args);}

--這里run方法返回的是ConfigurableApplicationContext

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args);}

public ConfigurableApplicationContext run(String... args) { ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); this.configureHeadlessProperty(); SpringApplicationRunListeners listeners = this.getRunListeners(args); listeners.starting(); Collection exceptionReporters; try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); this.configureIgnoreBeanInfo(environment); //打印banner,這里你可以自己涂鴉一下,換成自己項目的logo Banner printedBanner = this.printBanner(environment); //創建應用上下文 context = this.createApplicationContext(); exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context); //預處理上下文 this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); //刷新上下文 this.refreshContext(context); //再刷新上下文 this.afterRefresh(context, applicationArguments); listeners.started(context); this.callRunners(context, applicationArguments); } catch (Throwable var10) { } try { listeners.running(context); return context; } catch (Throwable var9) { }}

既然我們想知道tomcat在SpringBoot中是怎么啟動的,那么run方法中,重點關注創建應用上下文(createApplicationContext)和刷新上下文(refreshContext)。

創建上下文

//創建上下文protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch(this.webApplicationType) { case SERVLET: //創建AnnotationConfigServletWebServerApplicationContextcontextClass = Class.forName('org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext'); break; case REACTIVE: contextClass = Class.forName('org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext'); break; default: contextClass = Class.forName('org.springframework.context.annotation.AnnotationConfigApplicationContext'); } } catch (ClassNotFoundException var3) { throw new IllegalStateException('Unable create a default ApplicationContext, please specify an ApplicationContextClass', var3); } } return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);}

這里會創建AnnotationConfigServletWebServerApplicationContext類。而AnnotationConfigServletWebServerApplicationContext類繼承了ServletWebServerApplicationContext,而這個類是最終集成了AbstractApplicationContext。

刷新上下文

//SpringApplication.java//刷新上下文private void refreshContext(ConfigurableApplicationContext context) { this.refresh(context); if (this.registerShutdownHook) { try { context.registerShutdownHook(); } catch (AccessControlException var3) { } }}

//這里直接調用最終父類AbstractApplicationContext.refresh()方法protected void refresh(ApplicationContext applicationContext) { ((AbstractApplicationContext)applicationContext).refresh();}//AbstractApplicationContext.javapublic void refresh() throws BeansException, IllegalStateException { synchronized(this.startupShutdownMonitor) { this.prepareRefresh(); ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory(); this.prepareBeanFactory(beanFactory); try { this.postProcessBeanFactory(beanFactory); this.invokeBeanFactoryPostProcessors(beanFactory); this.registerBeanPostProcessors(beanFactory); this.initMessageSource(); this.initApplicationEventMulticaster(); //調用各個子類的onRefresh()方法,也就說這里要回到子類:ServletWebServerApplicationContext,調用該類的onRefresh()方法 this.onRefresh(); this.registerListeners(); this.finishBeanFactoryInitialization(beanFactory); this.finishRefresh(); } catch (BeansException var9) { this.destroyBeans(); this.cancelRefresh(var9); throw var9; } finally { this.resetCommonCaches(); } }}

//ServletWebServerApplicationContext.java//在這個方法里看到了熟悉的面孔,this.createWebServer,神秘的面紗就要揭開了。protected void onRefresh() { super.onRefresh(); try { this.createWebServer(); } catch (Throwable var2) { }}//ServletWebServerApplicationContext.java//這里是創建webServer,但是還沒有啟動tomcat,這里是通過ServletWebServerFactory創建,那么接著看下ServletWebServerFactoryprivate void createWebServer() { WebServer webServer = this.webServer; ServletContext servletContext = this.getServletContext(); if (webServer == null && servletContext == null) { ServletWebServerFactory factory = this.getWebServerFactory(); this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()}); } else if (servletContext != null) { try { this.getSelfInitializer().onStartup(servletContext); } catch (ServletException var4) { } } this.initPropertySources();}//接口public interface ServletWebServerFactory { WebServer getWebServer(ServletContextInitializer... initializers);}//實現AbstractServletWebServerFactoryJettyServletWebServerFactoryTomcatServletWebServerFactoryUndertowServletWebServerFactory

這里ServletWebServerFactory接口有4個實現類

SpringBoot內置tomcat啟動原理詳解

而其中我們常用的有兩個:TomcatServletWebServerFactory和JettyServletWebServerFactory。

//TomcatServletWebServerFactory.java//這里我們使用的tomcat,所以我們查看TomcatServletWebServerFactory。到這里總算是看到了tomcat的蹤跡。@Overridepublic WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir('tomcat'); tomcat.setBaseDir(baseDir.getAbsolutePath()); //創建Connector對象 Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); return getTomcatWebServer(tomcat);}protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) { return new TomcatWebServer(tomcat, getPort() >= 0);} //Tomcat.java//返回Engine容器,看到這里,如果熟悉tomcat源碼的話,對engine不會感到陌生。public Engine getEngine() { Service service = getServer().findServices()[0]; if (service.getContainer() != null) {return service.getContainer(); } Engine engine = new StandardEngine(); engine.setName( 'Tomcat' ); engine.setDefaultHost(hostname); engine.setRealm(createDefaultRealm()); service.setContainer(engine); return engine;}//Engine是最高級別容器,Host是Engine的子容器,Context是Host的子容器,Wrapper是Context的子容器

getWebServer這個方法創建了Tomcat對象,并且做了兩件重要的事情:把Connector對象添加到tomcat中,configureEngine(tomcat.getEngine()); getWebServer方法返回的是TomcatWebServer。

//TomcatWebServer.java//這里調用構造函數實例化TomcatWebServerpublic TomcatWebServer(Tomcat tomcat, boolean autoStart) { Assert.notNull(tomcat, 'Tomcat Server must not be null'); this.tomcat = tomcat; this.autoStart = autoStart; initialize();}private void initialize() throws WebServerException { //在控制臺會看到這句日志 logger.info('Tomcat initialized with port(s): ' + getPortsDescription(false)); synchronized (this.monitor) { try { addInstanceIdToEngineName(); Context context = findContext(); context.addLifecycleListener((event) -> { if (context.equals(event.getSource()) && Lifecycle.START_EVENT.equals(event.getType())) { removeServiceConnectors(); } }); //===啟動tomcat服務=== this.tomcat.start(); rethrowDeferredStartupExceptions(); try { ContextBindings.bindClassLoader(context, context.getNamingToken(), getClass().getClassLoader()); } catch (NamingException ex) { }//開啟阻塞非守護進程 startDaemonAwaitThread(); } catch (Exception ex) { stopSilently(); destroySilently(); throw new WebServerException('Unable to start embedded Tomcat', ex); } }}//Tomcat.javapublic void start() throws LifecycleException { getServer(); server.start();}//這里server.start又會回到TomcatWebServer的public void stop() throws LifecycleException { getServer(); server.stop();}

//TomcatWebServer.java//啟動tomcat服務@Overridepublic void start() throws WebServerException { synchronized (this.monitor) { if (this.started) { return; } try { addPreviouslyRemovedConnectors(); Connector connector = this.tomcat.getConnector(); if (connector != null && this.autoStart) { performDeferredLoadOnStartup(); } checkThatConnectorsHaveStarted(); this.started = true; //在控制臺打印這句日志,如果在yml設置了上下文,這里會打印 logger.info('Tomcat started on port(s): ' + getPortsDescription(true) + ' with context path ’' + getContextPath() + '’'); } catch (ConnectorStartFailedException ex) { stopSilently(); throw ex; } catch (Exception ex) { throw new WebServerException('Unable to start embedded Tomcat server', ex); } finally { Context context = findContext(); ContextBindings.unbindClassLoader(context, context.getNamingToken(), getClass().getClassLoader()); } }}//關閉tomcat服務@Overridepublic void stop() throws WebServerException { synchronized (this.monitor) { boolean wasStarted = this.started; try { this.started = false; try { stopTomcat(); this.tomcat.destroy(); } catch (LifecycleException ex) { } } catch (Exception ex) { throw new WebServerException('Unable to stop embedded Tomcat', ex); } finally { if (wasStarted) { containerCounter.decrementAndGet(); } } }}

附:tomcat頂層結構圖

SpringBoot內置tomcat啟動原理詳解

tomcat最頂層容器是Server,代表著整個服務器,一個Server包含多個Service。從上圖可以看除Service主要包括多個Connector和一個Container。Connector用來處理連接相關的事情,并提供Socket到Request和Response相關轉化。Container用于封裝和管理Servlet,以及處理具體的Request請求。那么上文提到的Engine>Host>Context>Wrapper容器又是怎么回事呢? 我們來看下圖:

SpringBoot內置tomcat啟動原理詳解

綜上所述,一個tomcat只包含一個Server,一個Server可以包含多個Service,一個Service只有一個Container,但有多個Connector,這樣一個服務可以處理多個連接。 多個Connector和一個Container就形成了一個Service,有了Service就可以對外提供服務了,但是Service要提供服務又必須提供一個宿主環境,那就非Server莫屬了,所以整個tomcat的聲明周期都由Server控制。

總結

SpringBoot的啟動主要是通過實例化SpringApplication來啟動的,啟動過程主要做了以下幾件事情:配置屬性、獲取監聽器,發布應用開始啟動事件初、始化輸入參數、配置環境,輸出banner、創建上下文、預處理上下文、刷新上下文、再刷新上下文、發布應用已經啟動事件、發布應用啟動完成事件。在SpringBoot中啟動tomcat的工作在刷新上下這一步。而tomcat的啟動主要是實例化兩個組件:Connector、Container,一個tomcat實例就是一個Server,一個Server包含多個Service,也就是多個應用程序,每個Service包含多個Connector和一個Container,而一個Container下又包含多個子容器。

到此這篇關于SpringBoot內置tomcat啟動原理詳解的文章就介紹到這了,更多相關SpringBoot內置tomcat啟動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产极品一区| 国产精品magnet| 国产精品主播| 男女精品网站| 中文另类视频| 成人一二三区| 麻豆国产91在线播放| 激情91久久| 欧美一级精品| 精品日韩视频| 四季av一区二区凹凸精品| 国产精品chinese| 日韩不卡一二三区| 亚洲欧洲日韩| 亚洲午夜免费| 国产免费成人| 私拍精品福利视频在线一区| 精品资源在线| 国产精品第一国产精品| 日韩精品视频网站| 欧美在线观看视频一区| 欧洲精品一区二区三区| 欧美成a人片免费观看久久五月天| 日韩有吗在线观看| 亚洲香蕉久久| 免费看日韩精品| 天堂va蜜桃一区二区三区| 久久国产福利| 综合五月婷婷| 日本va欧美va瓶| 欧美一区二区三区免费看| 欧美在线不卡| 国产精成人品2018| 久久精品二区亚洲w码| 国产欧美日韩精品一区二区三区| 欧美亚洲tv| 国产精品4hu.www| 久久狠狠久久| 久久不见久久见中文字幕免费| 国产精品jk白丝蜜臀av小说| 美女久久久精品| 国产高潮在线| 91精品一区二区三区综合在线爱| 99精品综合| 老牛影视一区二区三区| 亚洲伊人影院| 91亚洲精品视频在线观看| 青草国产精品久久久久久| 国产精品va视频| av高清不卡| 精品中文一区| 亚洲18在线| 日韩va欧美va亚洲va久久| 国产精品高清一区二区| av在线最新| 怡红院精品视频在线观看极品| 视频一区二区欧美| 欧美精品国产白浆久久久久| 精品久久中文| 免费观看久久av| 综合视频一区| 国产精品欧美大片| 不卡一二三区| 香蕉精品999视频一区二区| 色8久久久久| 免费一级欧美片在线观看网站 | 91综合网人人| 91精品亚洲| 亚洲欧美专区| 欧美成人aaa| 欧美91精品| 日韩精品91亚洲二区在线观看| 久久精品午夜| 精品91久久久久| 国产精品伦一区二区| 日韩欧美三级| 亚洲精品欧美| 日本在线啊啊| 午夜一级久久| 国产精品白丝久久av网站| 日韩精品dvd| 亚洲一区二区三区久久久| 国产麻豆精品久久| 欧美日韩一二三四| 青青国产精品| 99久久亚洲精品| 亚洲人成亚洲精品| 天堂av在线| 亚洲男人在线| 日韩av福利| 欧美在线观看天堂一区二区三区| 日韩精品一区二区三区免费观看| 亚洲精品美女| 欧美国产小视频| 亚洲久久在线| 成人久久一区| 欧美在线看片| 国产精品88久久久久久| 国产精品成人3p一区二区三区| 欧美成人高清| 欧美成人精品一级| 一区二区三区四区在线观看国产日韩| 国产一区二区三区四区五区传媒 | 成人在线丰满少妇av| 国产精品日韩久久久| 国产精品成人a在线观看| 狠狠干综合网| 国产美女高潮在线| 国产图片一区| 中文字幕一区日韩精品| 91精品一区二区三区综合| 国产精品美女午夜爽爽| 亚洲深夜影院| 欧美日韩免费观看视频| 国产午夜精品一区在线观看| 黄色日韩在线| 中文字幕在线免费观看视频| 日本亚洲不卡| 亚洲作爱视频| 99精品视频在线| 精品久久中文| 亚洲精品成人| 97国产成人高清在线观看| 日产欧产美韩系列久久99| 日韩免费久久| 日韩激情网站| 在线视频观看日韩| 成人国产精品一区二区免费麻豆| 亚洲一区二区av| 激情六月综合| 成人亚洲精品| 久久99青青| 亚洲精品裸体| 色婷婷精品视频| 国产精品hd| 亚洲精品进入| 激情欧美丁香| 91久久午夜| 欧美精品激情| 欧美日韩水蜜桃| 欧美一区久久久| 精品国产午夜| 另类欧美日韩国产在线| 国产精品视频一区二区三区| 欧美视频久久| 欧美日韩一区二区国产| 日韩av中文字幕一区二区| 亚洲日产av中文字幕| 亚洲激情五月| 亚洲国产专区| 亚洲精品888| 日韩毛片在线| 欧美久久亚洲| 日韩三区四区| 欧美一区二区三区久久精品| 亚洲欧美在线综合| 亚洲免费福利一区| 日本不卡高清| 91九色综合| 国产欧美一区二区色老头| 日韩va欧美va亚洲va久久| 日韩成人av影视| 国产精品亚洲综合在线观看| 国产精品玖玖玖在线资源| 国产精品久久久久久久久久妞妞| 91欧美精品| 日韩一二三区在线观看| 午夜精品福利影院| 911精品国产| 国产三级精品三级在线观看国产| 日韩欧美中文字幕电影| 日韩精品一区二区三区av| 国产日韩在线观看视频| 国产精品一区二区美女视频免费看| 国产免费av一区二区三区| 国产精品对白| 9999国产精品| 亚洲一级黄色| 亚洲午夜免费| 国产欧美日韩综合一区在线播放| 国产精品观看| 精品九九在线| 99久久精品费精品国产| 亚洲一区久久| 视频一区中文字幕精品| 欧美一区自拍| 水蜜桃久久夜色精品一区| 播放一区二区| 五月天久久久| 在线亚洲免费| 久久中文字幕一区二区三区| 国产日韩在线观看视频| 久久精品国产999大香线蕉| 裤袜国产欧美精品一区| 91精品电影| 中文字幕视频精品一区二区三区| 日本成人在线视频网站| 精品国产欧美| 99视频一区| 国产欧美日韩综合一区在线播放|