Spring boot啓動加載嵌入的tomcat過程:
1、執行SpringApplication.run方法。進入這個方法,會有一個refreshContext(context);方法。其中的context是spring boot自定義的一個類EmbeddedWebApplicationContext繼承了AbstractApplicationContext(); 重寫封裝了其中的onRefresh和finishRefresh方法。
2、開始執行context覆蓋的refresh方法,其實內部就是去執行父級的refresh方法。
3、執行this.onRefresh方法,這個是有自定義的context類覆蓋重寫了。
先執行父級的方法,再創建一個servletContainer容器。這裏獲取容器有jetty ,tomcat,undertow三種容器的實現。
進入到TomcatEmbeddedServletContainerFactory中,會有new Tomcat和設置端口等信息。
4、執行AbstractApplicationContext的this.finishRefresh方法,自定義的context覆蓋了該方法。
調用TomcatEmbeddedServletContainer的start()方法,裏面會執行startConnector方法執行tomcat的啓動。