8.1. SpringBoot启动流程--把spring应用跑起来

start方法里的源码

public void start() throws IllegalStateException {
        this.start("");
    }

    public void start(String taskName) throws IllegalStateException {
        if (this.currentTaskName != null) {
            throw new IllegalStateException("Can't start StopWatch: it's already running");
        } else {
            //给stopWatch对象保存一些信息
            this.currentTaskName = taskName;//当前任务的名字
            this.startTimeNanos = System.nanoTime();//当前的时间
        }

createBootstrapContext创建引导上下文

private DefaultBootstrapContext createBootstrapContext() {
        //创建默认的引导上下文对象
        //以后就会给里面保存很多信息
        DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
//遍历所有之前的获取到的bootstrappers,调用每一个bootstrappers的intitialize() 来完成对引导启动器上下文环境设置
        this.bootstrappers.forEach((initializer) -> initializer.intitialize(bootstrapContext));
        return bootstrapContext;//null
    }
=============================================================
public interface Bootstrapper {

    /**
     * Initialize the given {@link BootstrapRegistry} with any required registrations.
     * @param registry the registry to initialize
     */
//传入引导注册工厂
//这个工厂里面可以给他加入一些当前上下文的一些引导环境信息
    void intitialize(BootstrapRegistry registry);

}

让当前应用进入headless模式

private void configureHeadlessProperty() {
        System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
                System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
    }

获取所有RunListener(运行监听器)
所谓的监听器就是监听到当前项目的状态

private SpringApplicationRunListeners getRunListeners(String[] args) {
        Class[] types = new Class[] { SpringApplication.class, String[].class };
//new SpringApplicationRunListeners:相当于把找到的Listener保存到SpringApplicationRunListener;
        return new SpringApplicationRunListeners(logger,
//getSpringFactoriesInstances: 去spring.factories找SpringApplicationRunListener
                 getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args),
                this.applicationStartup);
    }

=====================================================
SpringApplicationRunListeners(Log log, Collection listeners,
            ApplicationStartup applicationStartup) {
        this.log = log;
        this.listeners = new ArrayList<>(listeners);
        this.applicationStartup = applicationStartup;
    }

List listeners;

image.png

starting():
遍历所有的SpringApplicationRunListener调用 starting 方法;相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。(即告诉我这个项目正在启动)

void starting(ConfigurableBootstrapContext bootstrapContext, Class mainApplicationClass) {
        doWithListeners("spring.boot.application.starting", (listener) -> listener.starting(bootstrapContext),
                (step) -> {
                    if (mainApplicationClass != null) {
                        step.tag("mainApplicationClass", mainApplicationClass.getName());
                    }
                });
    }

=========================================
进入doWithListeners方法:
private void doWithListeners(String stepName, Consumer listenerAction,
            Consumer stepAction) {
        StartupStep step = this.applicationStartup.start(stepName);
        //遍历所有的listeners,现在进行到哪一步了,相当于一个事件信息
        this.listeners.forEach(listenerAction);
        if (stepAction != null) {
            stepAction.accept(step);
        }
        step.end();
    }

================================
进入this.listeners.forEach(listenerAction):
void starting(ConfigurableBootstrapContext bootstrapContext, Class mainApplicationClass) {
//遍历所有的SpringApplicationRunListener调用starting方法
        doWithListeners("spring.boot.application.starting", (listener) -> listener.starting(bootstrapContext),
                (step) -> {
                    if (mainApplicationClass != null) {
                        step.tag("mainApplicationClass", mainApplicationClass.getName());
                    }
                });
    }

你可能感兴趣的:(springbootjava)