SpringCloud启动中SpringApplication构造方法执行多次

前言

最近在看源码的时候发现一个比较疑惑的地方就就是我启动spring boot会出现springApplication的构造方法和run方法多次调用。因此决定研究一下

正文

以下是我的启动代码

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
                SpringApplication springApplication = new SpringApplication(TestApplication .class);
                ConfigurableApplicationContext context = springApplication.run(args);
                context.close();
    }
}

经过debug发现以下流程

1 第一次调用

1.1 调用构造方法
1.2 执行run方法中的listeners.starting()时 再次调用构造方法

2 第二次调用

2.1 调用构造方法
2.2 执行run方法中的ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments)时; 再次调用构造方法

3 第三次调用

3.1 调用构造方法和run方法(完成后返回之前的run方法继续执行)
3.2 启动容器

探究

根据粗略的跟踪源码发现以上三个流程让我们仔细深入进去探究一下

RestartApplicationListener

由1.2可知第一次跳转是执行listeners.starting()时进行的
可能是因为启动RestartApplicationListener监听并传播ApplicationStartingEvent事件时创建了Restarter对象并重重启。
关键代码如下首先是RestartApplicationListener的onApplicationStartingEvent方法处理spring容器启动事件

 private void onApplicationStartingEvent(ApplicationStartingEvent event) {
        String enabled = System.getProperty("spring.devtools.restart.enabled");
        if (enabled != null && !Boolean.parseBoolean(enabled)) {
            Restarter.disable();
        } else {
            String[] args = event.getArgs();
            DefaultRestartInitializer initializer = new DefaultRestartInitializer();
            boolean restartOnInitialize = !AgentReloader.isActive();
            Restarter.initialize(args, false, initializer, restartOnInitialize);
        }

    }

以上通过 Restarter.initialize(args, false, initializer, restartOnInitialize);这行代码进行Restarter初始化启动的代码,让我们接着往下

public static void initialize(String[] args, boolean forceReferenceCleanup, RestartInitializer initializer, boolean restartOnInitialize) {
        Restarter localInstance = null;
        Object var5 = INSTANCE_MONITOR;
        synchronized(INSTANCE_MONITOR) {
            if (instance == null) {
               //进行 Restarter的初始化操作
                localInstance = new Restarter(Thread.currentThread(), args, forceReferenceCleanup, initializer);
                instance = localInstance;
            }
        }

        if (localInstance != null) {
            localInstance.initialize(restartOnInitialize);
        }

    }

以上代码主要进行了Restarter的初始化操作并且通过 localInstance.initialize(restartOnInitialize);来进行重启

protected void initialize(boolean restartOnInitialize) {
        this.preInitializeLeakyClasses();
        if (this.initialUrls != null) {
            this.urls.addAll(Arrays.asList(this.initialUrls));
            if (restartOnInitialize) {
                this.logger.debug("Immediately restarting application");
                this.immediateRestart();
            }
        }

    }

 private void immediateRestart() {
        try {
            this.getLeakSafeThread().callAndWait(() -> {
                this.start(FailureHandler.NONE);
                this.cleanupCaches();
                return null;
            });
        } catch (Exception var2) {
            this.logger.warn("Unable to initialize restarter", var2);
        }

        SilentExitExceptionHandler.exitCurrentThread();
    }

以上代码关键的地方位判断是否是restart初始化 如果是则需要重启 并且调用LeakSafeThread进行重启
关键方法为this.start(FailureHandler.NONE); 让我们接着跟踪

    protected void start(FailureHandler failureHandler) throws Exception {
        Throwable error;
        do {
            error = this.doStart();
            if (error == null) {
                return;
            }
        } while(failureHandler.handle(error) != Outcome.ABORT);

    }

    private Throwable doStart() throws Exception {
        Assert.notNull(this.mainClassName, "Unable to find the main class to restart");
        URL[] urls = (URL[])this.urls.toArray(new URL[0]);
        ClassLoaderFiles updatedFiles = new ClassLoaderFiles(this.classLoaderFiles);
        ClassLoader classLoader = new RestartClassLoader(this.applicationClassLoader, urls, updatedFiles, this.logger);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Starting application " + this.mainClassName + " with URLs " + Arrays.asList(urls));
        }

        return this.relaunch(classLoader);
    }

    protected Throwable relaunch(ClassLoader classLoader) throws Exception {
        RestartLauncher launcher = new RestartLauncher(classLoader, this.mainClassName, this.args, this.exceptionHandler);
        launcher.start();
        launcher.join();
        return launcher.getError();
    }

这里主要是启动RestartLauncher线程然后通过RestartLauncher线程的run方法进行重启 如下代码

public void run() {
        try {
            Class<?> mainClass = this.getContextClassLoader().loadClass(this.mainClassName);
            Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
            mainMethod.invoke((Object)null, this.args);
        } catch (Throwable var3) {
            this.error = var3;
            this.getUncaughtExceptionHandler().uncaughtException(this, var3);
        }

    }

如上RestartLauncher线程的run方法主要通过调用springboot启动类的main方法重新启动并刷新(因此此时已经把Restarter实例存入Restarter类中的instance中所以下次不会再初始化和重新构造了 因为没有重启jvm所以static中的数据没被刷新 所以存在)
以上为第一次调整并调用构造方法的原因和过程之后会再开章节详细讲解Restarter这个类的作用

2.2 BootstrapApplicationListener

第二次重启根据debug可以知道是通过调用run方法中的如下代码进行跳转的

this.prepareEnvironment(listeners, applicationArguments)

根据跟踪源码可以知道 这里不是重启,是在当前线程向 BootstrapApplicationListener发送一个ApplicationEnvironmentPreparedEvent广播,然后由BootstrapApplicationListener创建bootstrap上下文并返回

首先我们看BootstrapApplicationListener构造bootstrap的核心代码

        SpringApplicationBuilder builder = (new SpringApplicationBuilder(new Class[0])).profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF).environment(bootstrapEnvironment).registerShutdownHook(false).logStartupInfo(false).web(WebApplicationType.NONE);
        if (environment.getPropertySources().contains("refreshArgs")) {
            builder.application().setListeners(this.filterListeners(builder.application().getListeners()));
        }
        省略代码
        builder.sources((Class[])sources.toArray(new Class[sources.size()]));
        ConfigurableApplicationContext context = builder.run(new String[0]);
        context.setId("bootstrap");

由上面源码可知在BootstrapApplicationListener中会调用构造方法和run方法进行创建bootstrap上下文 然后 将上下文返回,并继续执行原来的run方法来创建application上下文

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数据结构 数据元素 是数据的基本单位,即数据集合中的个体。 一个数据元素可以由若干 数据项(Data Item) ...
    Cytosine阅读 305评论 0 1
  • 如果有一天我沉默了, 你会不会想到曾经的反驳。 如果有一天我崩溃了, 你会不会来了解一下。 如果有一天...
    忆往昔拾忆阅读 608评论 4 7