• 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

    在这里插入图片描述

  • 相关阅读:
    Nginx神奇的499竟然不在HTTP响应码标准内?快来了解一下!
    fabric.js的使用
    面试突击61:说一下MySQL事务隔离级别?
    C++——类型转换
    Spring JDBCTemplate简介
    Dubbo使用invoke指令来调用dubbo接口
    java基于微信小程序的大学生个人家庭理财产品 uniapp小程序
    驱动开发 day3 9/12
    Netty的高性能基石ByteBuf
    网络安全-Web端安全协议
  • 原文地址:https://blog.csdn.net/qq_37151886/article/details/124867335