SpringBoot的启动流程扩展点

阅读说明:
1.如果有排版格式问题,请移步https://www.yuque.com/mrhuang-ire4d/oufb8x/yo5ywqt5eudxvxfc?singleDoc#%20%E3%80%8ASpring%E5%8F%AF%E6%89%A9%E5%B1%95%E6%8E%A5%E5%8F%A3%E6%80%BB%E7%BB%93%E3%80%8B,选择宽屏模式效果更佳。
2.本文为原创文章,转发请注明出处。

SpringBoot的启动流程

启动源码如下:

    public ConfigurableApplicationContext run(String... args) {
        long startTime = System.nanoTime();
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting(bootstrapContext, this.mainApplicationClass);

        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            context.setApplicationStartup(this.applicationStartup);
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
            }

            listeners.started(context, timeTakenToStartup);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var12) {
            if (var12 instanceof SpringApplication.AbandonedRunException) {
                throw var12;
            }

            this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }

        try {
            if (context.isRunning()) {
                Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
                listeners.ready(context, timeTakenToReady);
            }

            return context;
        } catch (Throwable var11) {
            if (var11 instanceof SpringApplication.AbandonedRunException) {
                throw var11;
            } else {
                this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
                throw new IllegalStateException(var11);
            }
        }
    }

上下文准备

调用扩展点:ApplicationContextInitializer
发布上下文初始化事件:ApplicationContextInitializedEvent

上下文刷新

扫描注册beanDefinition;
创建bean流程;
发布上下文刷新事件:ContextRefreshedEvent

上下文刷新完成

发布上下文已启动事件:ApplicationStartedEvent
执行扩展点:ApplicationRunner,CommandLineRunner

最后

发布上下文ready事件:ApplicationReadyEvent

启动流程扩展点调用图:

 

更新信息请移步:Spring专栏
 

参考:
[1].https://blog.csdn.net/weixin_38650898/article/details/124320262
[2].https://mp.weixin.qq.com/s/dIv3FoLQuZRKpJNqo5H0LQ
%0A%0A%0A%0A%E5%8F%82%E8%80%83%EF%BC%9A%0A%5B1%5D.https%3A%2F%2Fblog.csdn.net%2Fweixin_38650898%2Farticle%2Fdetails%2F124320262%0A%5B2%5D.https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FdIv3FoLQuZRKpJNqo5H0LQ%0A%5B3%5D.
[3].https://www.cnblogs.com/myshare/p/17727492.html
[4].https://blog.csdn.net/ZXMSH/article/details/125587779

posted @ 2023-10-11 11:42  扎Zn了老Fe  阅读(22)  评论(0编辑  收藏  举报