当前位置: 首页 > news >正文

spring启动流程

Spring启动流程

随着springboot的功能越来越强大,我们逐渐忘记了spring,但是每当遇到问题时缺无从下手,
我们在享受springboot给我们带来的便利的同时更应该了解其底层原理,知其然更要知其所以然,下面我们一起进入spring的世界探索吧

Spring启动

我们从springboot的入口(不使用springboot的方式放在后面文章讲解)来看spring是如何被启动的
我们都有启动类如下

AttachCode

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

我们先来看下SpringApplication.run(Main.class, args);我们今天的主要内容不是springboot,所以我们一路跟踪到spring的refesh方法

AttachCode

   public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
    }

AttachCode

public void refresh(){
//省略部分代码
this.refreshContext(context);
//省略部分代码
}

AttachCode

    private void refreshContext(ConfigurableApplicationContext context) {
        if (this.registerShutdownHook) {
            shutdownHook.registerApplicationContext(context);
        }

        this.refresh(context);
    }

今天的主角AttachCode

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			// 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);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);
				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();

				// 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();
				contextRefresh.end();
			}
		}
	}

今天我们先到这里


本文由 idea的插件"AnNote"协助完成,是一款源码学习笔记,不但能够帮助我们做笔记、分享笔记还可以学习学霸的笔记。欢迎大家试用

相关文章:

  • 集成学习(上):Bagging集成方法
  • C# ManualResetEvent‌的高级用法
  • Pytorch学习笔记
  • 书摘 ASP.NET Core技术内幕与项目实战:基于DDD与前后端分离
  • 案例驱动的 IT 团队管理:创新与突破之路:第二章 团队组建:从人才画像到生态构建-2.2.1星型架构 vs 网状架构对比
  • 华为中小型企业项目案例
  • Three.js学习
  • 5分钟快速申请一个EDU教育邮箱
  • Python 编程题 第十节:重复数字、相邻字符去重、2的幂、最长公共子串、冒泡排序
  • 【2025年3月最新】Cities_Skylines:城市天际线1全DLC解锁下载与教程
  • vue中父组件与子组件的created方法执行顺序
  • Linux内核实时机制28 - RT调度器11 - RT 组调度
  • C# PaddleOCR字符识别
  • Git 使用指南
  • OSPF-5 3类LSA SummaryLSA
  • MySQL---DDL(3.17)
  • 【工作记录】F12查看接口信息及postman中使用
  • 【鸿蒙开发】Hi3861学习笔记- 定时器中断
  • 谷歌生态变革!Google Play宣布上线PC游戏平台
  • python中多重继承和泛型 作为模板让子类实现具体业务逻辑
  • 科普|“小”耳洞也会引发“大”疙瘩,如何治疗和预防?
  • 俄乌释放停火和谈信号,克宫:将组建“相应级别”谈判代表团
  • 《三餐四季》广东篇今晚开播:食在岭南,遇见百味
  • 哈尔滨工业大学原副校长王魁业逝世,享年92岁
  • 卢正已任上海市司法局党委委员、副局长
  • 印度军方否认S-400防空系统被摧毁