来看下在执行 run 方法到底有没有用到哪些自动配置的东西 , 我们点进 run:
public ConfigurableApplicationContext run(String... args) {//计时器StopWatch stopWatch = new StopWatch;stopWatch.start;ConfigurableApplicationContext context = ;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);//预刷新contextthis.prepareContext(context, environment, listeners, applicationArguments, printedBanner);//刷新contextthis.refreshContext(context);//刷新之后的contextthis.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);}try {listeners.running(context);return context;} catch (Throwable var9) {this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners));throw new IllegalStateException(var9);}}那我们关注的就是 refreshContext(context); 刷新 context , 我们点进来看 。
privatevoidrefreshContext(ConfigurableApplicationContext context) {refresh(context);if (this.registerShutdownHook) {try {context.registerShutdownHook;}catch (AccessControlException ex) {// Not allowed in some environments.}}}我们继续点进 refresh(context);
protected void refresh(ApplicationContext applicationContext) {Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);((AbstractApplicationContext) applicationContext).refresh;}他会调用 ((
AbstractApplicationContext) applicationContext).refresh;方法 , 我们点进来看:
public void refresh throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh;// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory;// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource;// Initialize event multicaster for this context.initApplicationEventMulticaster;// Initialize other special beans in specific context subclasses.onRefresh;// Check for listener beans and register them.registerListeners;// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh;}catch (BeansException ex) {if (logger.isWarnEnabled) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.destroyBeans;// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;}finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...resetCommonCaches;}}}由此可知 , 就是一个 spring 的 bean 的加载过程 。继续来看一个方法叫做 onRefresh:
protected void onRefresh throws BeansException {// For subclasses: do nothing by default.}他在这里并没有直接实现 , 但是我们找他的具体实现:

文章插图
比如 Tomcat 跟 web 有关 , 我们可以看到有个
ServletWebServerApplicationContext:
@OverrideprotectedvoidonRefresh {super.onRefresh;try {createWebServer;}catch (Throwable ex) {throw new ApplicationContextException("Unable to start web server", ex);}}可以看到有一个 createWebServer;方法他是创建web容器的 , 而Tomcat不就是web容器 , 那是如何创建的呢 , 我们继续看:
推荐阅读
- Spring IOC容器对Bean实例化的过程详解源码分析
- Attention 详解深度学习中的注意力机制
- 雪莲花的做法大全家常,简单易学槐花的四种家常做法详解
- 朱元璋:我能活多久,朱元璋八字分析
- 战国时期两个字的名人,战国时期两个水利工程
- 副清风散,详解鸡冠花的药用功效与作用
- 杭白菊老字号品牌,杭白菊的生长习性
- 适合水果商标名字,0万买回商标
- 亲自啃了一周,终于把Mybatis源码理清,以后简历请写精通二字
- Spring MVC 框架搭建配置方法及详解
