启动流程步骤开始:
1.首先点击run方法进入
SpringApplication.run(WechatPortalBootServletInitializer.class, args);
2.找到run方法的直接调用方法
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
**this.refreshContext(context);**
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
3.找到刷新上下文方法
this.refreshContext(context);
4.找到方法内部的
this.refresh(context);
private void refreshContext(ConfigurableApplicationContext context) {
**this.refresh(context);**
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
} catch (AccessControlException var3) {
}
}
}
5进入内部找到
((AbstractApplicationContext)applicationContext).refresh();
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
**((AbstractApplicationContext)applicationContext).refresh();**
}
6.点击进入refresh()方法,找到 this.onRefresh();
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
this.prepareRefresh();
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
this.initMessageSource();
this.initApplicationEventMulticaster();
**this.onRefresh();**
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}
7.找到onRefresh方法,他是一个抽象方法
protected void onRefresh() throws BeansException {
}
8.找到他的实现类(ctrl+alt+b)
![在这里插入图片描述](https://img-blog.csdnimg.cn/6b1d9e9488704d298895ebd28ab6f58b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5LiA5b6A5peg5YmN55qE6Zey6bG8,size_20,color_FFFFFF,t_70,g_se,x_16)
9.找到ServletWebServerApplicationContext实现类后,进入createWebServer方法
protected void onRefresh() {
super.onRefresh();
try {
**this.createWebServer();**
} catch (Throwable var2) {
throw new ApplicationContextException("Unable to start web server", var2);
}
}
10.进入内部的factory.getWebServer方法
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = this.getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = this.getWebServerFactory();
this.webServer = factory.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();
}
11.他是一个接口方法,找到实现类(ctrl +alt + b)
public interface ServletWebServerFactory {
WebServer getWebServer(ServletContextInitializer... initializers);
}
12. 终于找到我们想要看到的tomcat工厂方法了
![在这里插入图片描述](https://img-blog.csdnimg.cn/e1a9760409f74c51bffbb893e612a74a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5LiA5b6A5peg5YmN55qE6Zey6bG8,size_20,color_FFFFFF,t_70,g_se,x_16)
13.看到内部new Tomcat() 就在这里加载了上下文监听端口,创建连接等
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = this.baseDirectory != null ? this.baseDirectory : this.createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
this.customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
this.configureEngine(tomcat.getEngine());
Iterator var5 = this.additionalTomcatConnectors.iterator();
while(var5.hasNext()) {
Connector additionalConnector = (Connector)var5.next();
tomcat.getService().addConnector(additionalConnector);
}
this.prepareContext(tomcat.getHost(), initializers);
return this.getTomcatWebServer(tomcat);
}
14.start-web依赖加入后再lib包下就包含tomcat的jar包,再启动的时候springboot会自动加载对应的jar导入tomcat,有了tomcat再springboot启动加载监听玩所有的类配置文件后,一并放入tomcat服务器中启动,我们的服务就启动起来了
springboot的main方法启动原理源码跟踪
最新推荐文章于 2024-03-06 13:49:31 发布