拋出疑問
springboot-starter是依靠spring.factories和spring-autoconfigure-metadata.properties來進行啟動的. 那么springboot-starter-web是怎么搞的呢,我在下面并沒有找到spring.factories?是怎么區(qū)分Jetty,Tomcat,Undertow的呢递沪?又是怎么啟動的呢僚焦?
溯源
-
spring-autoconfigure包下的spring.factories中有 ServletWebServerFactoryAutoConfiguration,
這個類是啟動webServer的關鍵類:
1592561267587_514DCD41-59A6-4580-8634-E28387314496.png - 引入了jetty,tomcat,undertow的Bean细溅。
B574AE08-C657-417e-8F0F-B8A431775653.png
接下來這個Conditional就是classpath存在對應的類的時候加載對應的bean....于是乎我們大概懂了,當我們引入spring-starter-web 的時候,Tomcat.class就已經被引入了,所以這個bean就會被創(chuàng)建起來了~~~
-
那么快進到SpringBootApplication
B574AE08-C657-417e-8F0F-B8A431775653.png -
這里返回的applicationContext是AnnotationConfigServletWebServerApplicationContext
B574AE08-C657-417e-8F0F-B8A431775653.png -
再快進到 AbstractApplicationContext.refresh方法的onRefresh方法
B574AE08-C657-417e-8F0F-B8A431775653.png -
ServletWebServerApplicationContext的onRefresh方法,創(chuàng)建webServer
B574AE08-C657-417e-8F0F-B8A431775653.png -
這里的ServletWebServerFactory恰恰就是最上面的TomcatServletWebServerFactory...于是大概懂了是怎么整合的了
B574AE08-C657-417e-8F0F-B8A431775653.png