Spring Boot启动原理


源码分析jar为spring Boot 2.3.1

1.Spring Boot启动原理

  • 创建SpringApplication
    • 保存信息
    • 判断应用类型 是webflux还是servlet
    • ApplicationContextInitializer:去spring.factories找 ApplicationContextInitializer
    • ApplicationListener:应用监听器 去spring.factories找 ApplicationContextInitializer
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}
  • 运行SpringApplication
    • stopWatch
    • 记录应用启动时间
    • 让当前应用进入headless java.awt.headless
    • 获取所有运行监听器getRunListeners(args);
      • 获取SpringApplicationRunListener
      • 遍历 start
      • 这里可以监听系统启动过程 可以自定义类
    • 保存命令行参数 applicationArguments
    • 准备环境prepareEnvironment
      • 得到基础环境信息StandardEnvironment
      • 配置基础环境信息
        • 读取所有配置文件 configurePropertySources
        • 激活环境 configureProfiles
      • 监听器调用listener.environmentPrepared();通知所有的监听器当前环境准备完成
    • 配置忽略bean的信息
    • 创建IOC容器
      • 根据类型创建不同的applicationcontext
    • 准备ApplicationContext IOC容器的基本信息 prepareContext()
      • 保存环境信息
      • 后置处理
      • 应用初始化:applyInitializers 对应创建SpringApplication创建的ApplicationContextInitializer
        • 调用initialize 对ioc容器进行初始化扩展
      • 监听器调用listener.contextPrepared()
      • 监听器调用listener.contextLoaded()
    • refreshContext():spring核心源码
    • afterRefresh(context, applicationArguments)
      • 也可以扩展自己的实现
    • 监听器调用listener.started()
    • 调用所有的runners
      • 获取容器中的ApplicationRunner
      • 获取容器中的CommandLineRunner
      • AnnotationAwareOrderComparator 排序合并
    • 监听器调用listener.running()
public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			listeners.started(context);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值