• Spring Boot启动流程分析


    SpringBoot版本2.4.2
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    1 StopWatch

    在这里插入图片描述
    创建并启动计时器
    在这里插入图片描述

    2 createBootstrapContext

    创建上下文
    在这里插入图片描述
    在这里插入图片描述

    configureHeadlessProperty

    设置系统属性 java.awt.headless
    在这里插入图片描述
    在这里插入图片描述

    getRunListeners

    获取运行监听
    在这里插入图片描述
    在这里插入图片描述

    starting

    监听启动
    在这里插入图片描述

    DefaultApplicationArguments

    默认应用参数
    在这里插入图片描述
    在这里插入图片描述

    prepareEnvironment

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

    在这里插入图片描述

    configureIgnoreBeanInfo

    在这里插入图片描述

    printBanner

    在这里插入图片描述

    createApplicationContext

    在这里插入图片描述

    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);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    refreshContext

    在这里插入图片描述

    afterRefresh

    在这里插入图片描述

    stop

    在这里插入图片描述

    listeners.started(context)

    在这里插入图片描述

    callRunners

    在这里插入图片描述

  • 相关阅读:
    口袋参谋:提高宝贝自然流量的三种方法!
    Remult:使用 TypeScript 构建一个类型安全的全栈应用程序
    【你不知道的javascript上】this指针,函数的call、apply、bind,new 操作符到底发生了什么
    关于语雀 23 日故障的公告
    长时间戴耳机耳朵不舒服?骨传导耳机可以缓解这个问题
    Android框架mqtt库无法兼容高版本android13的问题
    MySQL(4)
    计算机毕业设计ssm大学生比赛信息管理系统38iiq系统+程序+源码+lw+远程部署
    2023-09-07 LeetCode每日一题(修车的最少时间)
    蓝牙技术|蓝牙在物联网产品上的功能,特别是苹果Find My中的应用
  • 原文地址:https://blog.csdn.net/qq_37151886/article/details/124867335