spring boot 启动流程

计时器开始计时

headless模式赋值

发送ApplicationAStartingEvent

配置环境模块

发送ApplicationEnvironmentPreparedEvent

打印banner

创建上下文应用对象

初始化失败分析器

关联SpringBoot组件和应用上下文对象

发送ApplicationContextiNitializedEvent

加载sources到context

 发送applicationPreparedEvent

刷新上下文

计时器停止计时

发送applicationStartedEvent

调用框架启动扩展类

发送applicationReadyEvent

 

框架自动化装配步骤:

1)收集配置文件中的配置工厂类

2)加载组件工厂

3)注册组件内定义bean

 

public ConfigurableApplicationContext run(String... args) {
        //记录程序运行时间
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // ConfigurableApplicationContext Spring 的上下文
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        //从META-INF/spring.factories中获取监听器
        //1、获取并启动监听器
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            //2、将参数赋值进ApplicationArguments意味着这个类是用于存储run方法的参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            //3、构造容器环境
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            //处理需要忽略的Bean
            configureIgnoreBeanInfo(environment);
            //打印banner
            Banner printedBanner = printBanner(environment);
            ///3、初始化容器
            context = createApplicationContext();
            //实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,
                    new Class[]{ConfigurableApplicationContext.class}, context);
            //4、刷新容器前的准备阶段
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            //5、刷新容器
            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、付费专栏及课程。

余额充值