This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

Before through a short series of more text, let us know the automatic assembly of Spring Boot, but also to achieve their own starter, can be said to have a certain understanding of Spring Boot. Do you know what Spring Boot is? It’s… Don’t you just click run to launch it?



Let’s get to know how Spring Boot starts, and drop the link to the previous series for those of you who haven’t seen it

  • Spring Boot Review (5) : Implement the first custom starter
  • Spring Boot Review (part 4) : Learn more about SpringFactoriesLoader
  • Spring Boot Review (iii) : Autowire principle analysis
  • Spring Boot Review (2) : Multi-module bean injection
  • Spring Boot Review I: Implement your first custom annotations

Details of the run method

Let’s start with the startup class from the previous projectNotice that each of the main methods has a piece of code like this

SpringApplication.run(ClientApplication.class, args);
Copy the code

It’s pretty much a guess that the Start of the Spring Boot project must have started here, so let’s go down here. We are using the static run method of SpringApplication, so we first create an instance of the SpringApplication object in this method, and then call the instance method of the created SpringApplication.

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

Also, when a SpringApplication object instance is created, its own initialize method is called

 private void initialize(Object[] sources) {
        if(sources ! =null && sources.length > 0) {
            this.sources.addAll(Arrays.asList(sources));
        }
        this.webEnvironment = deduceWebEnvironment();
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = deduceMainApplicationClass();
    }
Copy the code

After the initialize method is executed, we start to get into the focus of the Spring BooT BooT process. At this point, we call our run method to start SpringBoot. The source code is as follows

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
    ConfigurableApplicationContext context = null;
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting(bootstrapContext, this.mainApplicationClass);

    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        this.configureIgnoreBeanInfo(environment);
        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);
        stopWatch.stop();
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context);
        this.callRunners(context, applicationArguments);
    } catch (Throwable var10) {
        this.handleRunFailure(context, var10, listeners);
        throw new IllegalStateException(var10);
    }

    try {
        listeners.running(context);
        return context;
    } catch (Throwable var9) {
        this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
        throw newIllegalStateException(var9); }}Copy the code

The whole method is still relatively long, let’s watch patiently. First, start our StopWatch timer to record the execution time of the task. Next we call createBootstrapContext, which basically initializes the launch context; Then carries on the system parameter setting configureHeadlessProperty; Then initialize the listener list, calling getRunListeners; After the listener list is started, publish the SpringBoot start event. Behind in the try block of code, mainly for the spring context of preparation, including to set up the environment variables, load ApplicationContextInitializer, complete corresponding bean registration operation, etc. Finally, all the listeners are notified that the Spring container is started

conclusion

Finally, we plot the flow of the entire run method with the flow

Graph TD Start --> Create and prepare environment parameters --> Create and initialize ApplicationContext--> Call the refresh method of ApplicationContext, complete startup --> Stop