The startup process is generally divided into two steps: the first step is the construction and initialization of the SpringApplication and the second step is to run the run method

The first step

Start with the startup class

@SpringBootApplication
public class CmsApplication {

    public static void main(String[] args) { SpringApplication.run(CmsApplication.class, args); }}Copy the code

The SpringBootApplication annotation is the integration of @Configuration,@EnableAutoConfiguration, and @ComponentScan annotations, respectively representing the Configuration bean of the Springbean. Turn on the automatic configuration spring context, which is where the component is scanned, which is why *application.java needs to be placed in the root path, so that @ComponentScan scans the entire project.

So let’s click on run

public static ConfigurableApplicationContext run(Class
        primarySource, String... args) {
    return run(new Class[]{primarySource}, args);
}

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

The first run will execute the second run, the second run will initialize SpringApplication, click on SpringApplication, and finally execute the constructor SpringApplication,

public SpringApplication(Class
       ... primarySources) {
    this((ResourceLoader)null, primarySources);
}

public SpringApplication(ResourceLoader resourceLoader, Class
       ... primarySources) {
    this.sources = new LinkedHashSet();
    this.bannerMode = Mode.CONSOLE;
    this.logStartupInfo = true;
    this.addCommandLineProperties = true;
    this.addConversionService = true;
    this.headless = true;
    this.registerShutdownHook = true;
    this.additionalProfiles = new HashSet();
    this.isCustomEnvironment = false;
    this.lazyInitialization = false;
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();
}
Copy the code

The initialize method is used to initialize the base value.

1. Manage the source in the Sources property of the SpringApplication, sources is a LinkedHashSet(), which means we can create multiple custom non-duplicate applications at the same time, but currently there is only one. 2. Determine whether web application (javax.mail. Servlet. Servlet and org. Springframework. Web. Context. ConfigurableWebApplicationContext must exist in the class loader). And set it to the webEnvironment property. 3. From the spring. Find ApplicationContextInitializer factories and set to the initializer initializers. Find the ApplicationListener from the Spring. factories directory, instantiate it, and set it to the Listener properties of the SpringApplication. The process is to find all the application event listeners. 5. Find the main method Class (in this case CmsApplication) and return the Class object.

So far the first step is complete.

The second step

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        / / create the application of the listener SpringApplicationRunListeners and began to listen
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        // iterate over the call
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // Load the SpringBoot configuration environment (ConfigurableEnvironment), or if published through the Web container, load StandardEnvironment, which eventually inherits ConfigurableEnvironment
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            // Prints the banner icon
            Banner printedBanner = this.printBanner(environment);
            // Create ApplicationContext and identify it as webService
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            The prepareContext method associates important components, such as Listeners, Environment, applicationArguments, and Banner, with context objects
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // The following refreshContext(context) method is the key to the spring-boot-starter-*(mybatis, redis, etc.) configuration. Bean instantiation and other core work.
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);// Issue the ApplicationStartedEvent event
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

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



Pictures from:www.cnblogs.com/theRhyme/p/…