美文网首页
SpringBoot的启动流程扩展点

SpringBoot的启动流程扩展点

作者: 扎Zn了老Fe | 来源:发表于2023-10-10 11:30 被阅读0次

    阅读说明:
    1.如果有排版格式问题,请移步https://www.yuque.com/mrhuang-ire4d/oufb8x/yo5ywqt5eudxvxfc?singleDoc# 《Spring可扩展接口总结》,选择宽屏模式效果更佳。
    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

    启动流程扩展点调用图:


    image.png

    更新信息请查看https://www.yuque.com/mrhuang-ire4d/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

    相关文章

      网友评论

          本文标题:SpringBoot的启动流程扩展点

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