1、org.springframework.boot.SpringApplication#initialize
java
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
加载了org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
2、spring refresh 中onRefresh回调中启动了内置Tomcat
ServletWebServerApplicationContext继承AbstractApplicationContext,并且重新实现了onRefresh方法。
java
//重写了refresh方法,启动
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
//启动Tomcat
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
//如果Tomcat启动了,就发布服务ServletWebServerInitializedEvent
@Override
protected void finishRefresh() {
super.finishRefresh();
WebServer webServer = startWebServer();
if (webServer != null) {
publishEvent(new ServletWebServerInitializedEvent(webServer, this));
}
}