[Spring 源码] 浅析 SpringApplication#run()
2023-12-13 05:59:45
    		Spring 5底层原理 系列的学习笔记
SpringApplication#run(java.lang.String...)
 
源码
    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 {
            // 封装启动 args
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //Environment 即环境对象,是对配置信息的抽象,配置信息的来源有多种,比如:
            //	系统环境变量、properties 配置文件、YAML 配置文件等等。
			// SpringBoot 提供了名为 ApplicationEnvironment 的类表示环境对象,
            //	它是 Spring 中 StandardEnvironment 环境对象的子类。
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            // 打印banner
            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) {
            this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }
        try {
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            listeners.ready(context, timeTakenToReady);
            return context;
        } catch (Throwable var11) {
            this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var11);
        }
    }
总结
- 得到 SpringApplicationRunListeners 事件发布器
发布 Application Starting 事件 1??
- 封装启动 args
- 准备 Environment 添加命令行参数
- ConfigurationPropertySources 处理
发布 Application Environment 已准备事件 2??
- 通过 EnvironmentPostProcessorApplicationListener 进行 env 后处理
application.properties 由 StandardConfigDataLocationResolver 解析
spring.application.json
- 绑定 spring.main 到 SpringApplication 对象
- 打印 Banner
- 创建容器
- 准备容器
发布 Application Context 已初始化事件 3??
- 加载 Bean 定义
发布 Application Prepared 事件 4??
- refresh 容器
发布 Application Started 事件 5??
- 执行 Runner
发布 Application Ready 事件 6??
这其中有异常,发布 Application Failed 事件 7??
    			文章来源:https://blog.csdn.net/qq_45704048/article/details/134961579
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
    	本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!