springboot初始化过程(源代码注释)

package com.lsb.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

springboot启动入口类

//SpringApplication.run()
public static ConfigurableApplicationContext run(Object source, String... args) {
        return run(new Object[]{source}, args);
    }
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
        return (new SpringApplication(sources)).run(args);
    }

调用了SpringApplication.run方法


//new SpringApplication(sources)
//一些基本配置的初始化
public SpringApplication(Object... sources) {
//banner就是springboot启动时的那个小图案...
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.initialize(sources);
    }

对一些基本的配置进行初始化,

  • 输出图案
  • 是否打印启动log
  • 命令行参数读取?
  • Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标
  • Java程序经常也会遇到进程挂掉的情况,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码。JAVA中的ShutdownHook提供了比较好的方案。
  • 额外的参数
  • 初始化
//初始化
private void initialize(Object[] sources) {
        if (sources != null && sources.length > 0) {
            this.sources.addAll(Arrays.asList(sources));
        }
//判断是不是web环境,还是标准环境

        this.webEnvironment = deduceWebEnvironment();
        setInitializers((Collection) getSpringFactoriesInstances(//
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//判断是不是从main方发中执行的程序
        this.mainApplicationClass = deduceMainApplicationClass();
    }
  • 此函数主要是对执行环境的一些判断以及初始化工作


    sources
("javax.servlet.Servlet",   "org.springframework.web.context.ConfigurableWebApplicationContext")

启动器通过反射判断有没有这两个类的的实现,如果有就是web环境,否则就是普通环境

//从函数调用栈中寻找main方法,如果找到,就把main方法的类对象返回
private Class<?> deduceMainApplicationClass() {
        try {
            StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
            for (StackTraceElement stackTraceElement : stackTrace) {
                if ("main".equals(stackTraceElement.getMethodName())) {
                    return Class.forName(stackTraceElement.getClassName());
                }
            }
        }
        catch (ClassNotFoundException ex) {
            // Swallow and continue
        }
        return null;
    }
  • 通过函数调用栈判断是不是从main方法中开始执行的陈股

//run
public ConfigurableApplicationContext run(String... args) {
//定时器,用于日志打印程序执行时间
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
//上下文
        ConfigurableApplicationContext context = null;
//失败分析工具
        FailureAnalyzers analyzers = null;
//Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。(java.awt.headless)
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
//main方法参数实例化
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//打印springboot图案
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            analyzers = new FailureAnalyzers(context);
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            listeners.finished(context, null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            return context;
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, analyzers, ex);
            throw new IllegalStateException(ex);
        }
    }
  • 对初始化进行的实际操作,执行初始化。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容