Ctrl+N 搜索 AbstractApplicationContext 里面的onRefresh()方法
protected void onRefresh() throws BeansException {
}
然后找到实现类ServletWebServerApplicationContext
protected void onRefresh() {
super.onRefresh();
try {
//
this.createWebServer();
} catch (Throwable var2) {
throw new ApplicationContextException("Unable to start web server", var2);
}
}
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = this.getServletContext();
if(webServer == null && servletContext == null) {
//
ServletWebServerFactory ex = this.getWebServerFactory();
//工厂获取webServer的
this.webServer = ex.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});
} else if(servletContext != null) {
try {
this.getSelfInitializer().onStartup(servletContext);
} catch (ServletException var4) {
throw new ApplicationContextException("Cannot initialize servlet context", var4);
}
}
this.initPropertySources();
}
我们再进入到getWebServer这个方法内部
public WebServer getWebServer(ServletContextInitializer... initializers) {
//创建一个tomcat对象
Tomcat tomcat = new Tomcat();
File baseDir = this.baseDirectory != null?this.baseDirectory:this.createTempDir("tomcat");
//设置baseDir
tomcat.setBaseDir(baseDir.getAbsolutePath());
//创建tomcat的连接器
Connector connector = new Connector(this.protocol);
//把连接器设置到tomcat中去
tomcat.getService().addConnector(connector);
this.customizeConnector(connector);
tomcat.setConnector(connector);
//设置为是否自动启动
tomcat.getHost().setAutoDeploy(false);
//创建tomcat引擎
this.configureEngine(tomcat.getEngine());
Iterator var5 = this.additionalTomcatConnectors.iterator();
while(var5.hasNext()) {
Connector additionalConnector = (Connector)var5.next();
tomcat.getService().addConnector(additionalConnector);
}
//刷新tomcat的上下文
this.prepareContext(tomcat.getHost(), initializers);
//准备启动(还未启动)
return this.getTomcatWebServer(tomcat);
}
然后进入准备启动的getTomcatWebServer()这个方法
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
return new TomcatWebServer(tomcat, this.getPort() >= 0);
}
这个autoStart是什么意思?端口号>0自动启动
public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
this.monitor = new Object();
this.serviceConnectors = new HashMap();
Assert.notNull(tomcat, "Tomcat Server must not be null");
this.tomcat = tomcat;
this.autoStart = autoStart;
//tomcat启动
this.initialize();
}
private void initialize() throws WebServerException {
logger.info("Tomcat initialized with port(s): " + this.getPortsDescription(false));
Object var1 = this.monitor;
synchronized(this.monitor) {
try {
this.addInstanceIdToEngineName();
//找到tomcat上下文
Context ex = this.findContext();
ex.addLifecycleListener((event) -> {
if(ex.equals(event.getSource()) && "start".equals(event.getType())) {
this.removeServiceConnectors();
}
});
//启动tomcat
this.tomcat.start();
this.rethrowDeferredStartupExceptions();
try {
ContextBindings.bindClassLoader(ex, ex.getNamingToken(), this.getClass().getClassLoader());
} catch (NamingException var5) {
;
}
//
this.startDaemonAwaitThread();
} catch (Exception var6) {
this.stopSilently();
throw new WebServerException("Unable to start embedded Tomcat", var6);
}
}
}
来源:oschina
链接:https://my.oschina.net/architectliuyuanyuan/blog/3188056