美文网首页
springboot 2.0启动流程源码分析(三)

springboot 2.0启动流程源码分析(三)

作者: kaizhi | 来源:发表于2018-12-25 11:47 被阅读4次

我们先回顾下第一篇文章里列出的springboot 启动过程:
1.获取并启动监听器
2.读取命令行的参数
3.构造容器环境
4.创建容器
5.实例化SpringBootExceptionReporter
6.准备容器
7.刷新容器
8.刷新容器后的扩展接口
上一篇文章我们分析了 springboot启动过程的第1步:获取并启动监听器,这篇文章我们来分析读取命令行的参数
再贴一下run函数的代码

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;
    }

相关文章

网友评论

      本文标题:springboot 2.0启动流程源码分析(三)

      本文链接:https://www.haomeiwen.com/subject/bznxlqtx.html