自动配置类
源码位置: org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 导入 tomcat,jetty,undertow 的配置
@Import ({ ServletWebServerFactoryAutoConfiguration . BeanPostProcessorsRegistrar . class ,
ServletWebServerFactoryConfiguration . EmbeddedTomcat . class ,
ServletWebServerFactoryConfiguration . EmbeddedJetty . class ,
ServletWebServerFactoryConfiguration . EmbeddedUndertow . class })
public class ServletWebServerFactoryAutoConfiguration {
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer ( ServerProperties serverProperties ,
ObjectProvider < WebListenerRegistrar > webListenerRegistrars ,
ObjectProvider < CookieSameSiteSupplier > cookieSameSiteSuppliers ) {
// 配置 serverProperties
return new ServletWebServerFactoryCustomizer ( serverProperties ,
webListenerRegistrars . orderedStream (). collect ( Collectors . toList ()),
cookieSameSiteSuppliers . orderedStream (). collect ( Collectors . toList ()));
}
...
}
创建 webServer
源码位置: org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh
1
2
3
4
5
6
7
8
9
10
11
@Override
protected void onRefresh () {
super . onRefresh ();
try {
// 创建 webServer
createWebServer ();
}
catch ( Throwable ex ) {
throw new ApplicationContextException ( "Unable to start web server" , ex );
}
}
源码位置: org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#createWebServer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void createWebServer () {
WebServer webServer = this . webServer ;
ServletContext servletContext = getServletContext ();
if ( webServer == null && servletContext == null ) {
// 获取 ServletWebServerFactory (在自动配置类中)
ServletWebServerFactory factory = getWebServerFactory ();
// 创建 webServer
this . webServer = factory . getWebServer ( getSelfInitializer ());
...
// 会执行回调来执行 webServer#start 方法
getBeanFactory (). registerSingleton ( "webServerStartStop" ,
new WebServerStartStopLifecycle ( this , this . webServer ));
}
...
}