乐趣区

关于springboot:81-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<? extends SpringApplicationRunListener> listeners,
            ApplicationStartup applicationStartup) {
        this.log = log;
        this.listeners = new ArrayList<>(listeners);
        this.applicationStartup = applicationStartup;
    }

List<SpringApplicationRunListener> listeners;

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<SpringApplicationRunListener> listenerAction,
            Consumer<StartupStep> 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());
                    }
                });
    }
退出移动版