刷新应用上下文就进入了spring的源码了
public void refresh() throws BeansException, IllegalStateException { synchronized(this.startupShutdownMonitor) { StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh"); // Prepare this context for refreshing. //准备刷新上下文 this.prepareRefresh(); // Tetl the subclass to refresh the internal bean facto // 通知子类刷新内部工厂 ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. // 准备Bean工厂 this.prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in contex t subc lasses. // 允许在上下文子类中对bean工厂进行后处理。 // Invoke factory processors registered as beans in the context, this.postProcessBeanFactory(beanFactory); StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process"); this.invokeBeanFactoryPostProcessors(beanFactory); // 注册后置处理器。 this.registerBeanPostProcessors(beanFactory); beanPostProcess.end(); // 初始化信息源 this.initMessageSource(); // 初始化上下文事件发布器 this.initApplicationEventMulticaster(); // 初始化其他自定义bean this.onRefresh(); // 注册监听器 this.registerListeners(); this.finishBeanFactoryInitialization(beanFactory); //完成刷新,清缓存,初始化生命周期,事件发布等 this.finishRefresh(); } catch (BeansException var10) { if (this.logger.isWarnEnabled()) { this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10); } // 销毁bean this.destroyBeans(); // Reset 'active'flag. this.cancelRefresh(var10); throw var10; } finally { this.resetCommonCaches(); contextRefresh.end(); } } }刷新的代码有点深,也是在这时创建了Tomcat对象,这也是SpringBoot** 一键启动**web工程的关键
创建了Tomcat对象,并设置参数 @Override public WebServer getWebServer(ServletContextInitializer... initializers) { if (this.disableMBeanRegistry) { Registry.disableRegistry(); } Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); connector.setThrowOnFailure(true); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); // 返回TomcatWebServer服务 return getTomcatWebServer(tomcat); } 8.刷新后处理
afterReftesh(); //是个一空实现,留着后期扩展
/** * Called after the context has been refreshed. * @param context the application context * @param args the application arguments */ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) { } 9.发布监听应用启动事件 @Override public void started(ConfigurableApplicationContext context) { context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context)); AvailabilityChangeEvent.publish(context, LivenessState.CORRECT); }这里是调用context.publishEvent()方法,发布应用启动事件ApplicationStartedEvent.
10.执行Runner