Spring Boot启动流程分析

  • A+
所属分类:Spring Boot

SpringBoot版本2.4.2
Spring Boot启动流程分析
Spring Boot启动流程分析
Spring Boot启动流程分析

run方法

public ConfigurableApplicationContext run(String... args) {
   
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();		
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();		
		ConfigurableApplicationContext context = null;		
		configureHeadlessProperty();		
		SpringApplicationRunListeners listeners = getRunListeners(args);		
		listeners.starting(bootstrapContext, this.mainApplicationClass);		
		try {
   
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
			prepareContext(bootstrapContext, 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, listeners);
			throw new IllegalStateException(ex);
		}
		try {
   
			listeners.running(context);
		}
		catch (Throwable ex) {
   
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

1 StopWatch

Spring Boot启动流程分析
创建并启动计时器
Spring Boot启动流程分析

2 createBootstrapContext

创建上下文
Spring Boot启动流程分析
Spring Boot启动流程分析

configureHeadlessProperty

设置系统属性 java.awt.headless
Spring Boot启动流程分析
Spring Boot启动流程分析

getRunListeners

获取运行监听
Spring Boot启动流程分析
Spring Boot启动流程分析

starting

监听启动
Spring Boot启动流程分析

DefaultApplicationArguments

默认应用参数
Spring Boot启动流程分析
Spring Boot启动流程分析

prepareEnvironment

根据监听、上下文以及参数准备环境

Spring Boot启动流程分析

configureIgnoreBeanInfo

Spring Boot启动流程分析

printBanner

Spring Boot启动流程分析

createApplicationContext

Spring Boot启动流程分析

prepareContext

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
			ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments, Banner printedBanner) {
   
		context.setEnvironment(environment);
		postProcessApplicationContext(context);
		applyInitializers(context);
		listeners.contextPrepared(context);
		bootstrapContext.close(context);
		if (this.logStartupInfo) {
   
			logStartupInfo(context.getParent() == null);
			logStartupProfileInfo(context);
		}
		// Add boot specific singleton beans
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
		if (printedBanner != null) {
   
			beanFactory.registerSingleton("springBootBanner", printedBanner);
		}
		if (beanFactory instanceof DefaultListableBeanFactory) {
   
			((DefaultListableBeanFactory) beanFactory)
					.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
		}
		if (this.lazyInitialization) {
   
			context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
		}
		// Load the sources
		Set<Object> sources = getAllSources();
		Assert.notEmpty(sources, "Sources must not be empty");
		load(context, sources.toArray(new Object[0]));
		listeners.contextLoaded(context);
	}

refreshContext

Spring Boot启动流程分析

afterRefresh

Spring Boot启动流程分析

stop

Spring Boot启动流程分析

listeners.started(context)

Spring Boot启动流程分析

callRunners

Spring Boot启动流程分析

w3cjava

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: