SpringBoot内置Tomcat启动的源码分析

  1. 完整启动debug流程如下:
>SpringApplication.run(BootStudyApplication.class,args);
 	>org.springframework.boot.SpringApplication#run(java.lang.Class<?>[], java.lang.String[])
 		>org.springframework.boot.SpringApplication#refreshContext
 			>org.springframework.boot.SpringApplication#refresh
 				>org.springframework.context.support.AbstractApplicationContext#refresh
 					>org.springframework.context.support.AbstractApplicationContext#refresh
 						>org.springframework.context.support.AbstractApplicationContext#onRefresh
 							>org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#createWebServer
 								>org.springframework.boot.web.servlet.server.ServletWebServerFactory#getWebServer
 									>org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getTomcatWebServer
 										>org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getTomcatWebServer
 											>org.springframework.boot.web.embedded.tomcat.TomcatWebServer#initialize
 												>org.springframework.boot.web.embedded.tomcat.TomcatWebServer#start

描述:
SpringBoot项目启动会调用Spring刷新容器方法,然后会调用到onRefresh方法,此方法是一个空方法,被子类ServletWebServerApplicationContext覆写,然后他会调用创建web容器的方法,然后创建出一个Tomcat,并且启动Tomcat。

流程图如下:

  1. 有兴趣自己调试的小伙伴可以写一个SpringBoot项目,把断点打在TomcatWebServer的start方法上,然后往上翻代码即可。
原文地址:https://www.cnblogs.com/javammc/p/12590155.html