Implementation principle

Posted Jun 28, 20203 min read

2.4 Implementation Principle

Each Spring Boot project has a main program startup class. In the main program startup class, there is a main() method to start the project. In this method, the entire Spring Boot program can be started by executing

Question:So how does the method actually start the Spring Boot project?

Below we look at the source code inside the run() method, the core code is as follows:



public class SpringbootDemoApplication {

public static void main(String[]args) {, args);





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);



It can be seen from the above source code that the method performs two operations internally, namely the initial creation of the SpringApplication instance and the call to run() to start the project. The specific implementation of these two stages is as follows

**1. Initial creation of SpringApplication instance**

View the source code information created by the initialization of the SpringApplication instance object, the core code is as follows


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.resourceLoader = resourceLoader;

Assert.notNull(primarySources, "PrimarySources must not be null");

//Set the project startup class.class as an attribute and store it

this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));

//Determine the type of the current webApplicationType application

this.webApplicationType = WebApplicationType.deduceFromClasspath();

//Set the initializer(Initializer), and finally call these initializers



//Set the listener(Listener)


//The main program startup class used to infer and set the project main() method to start

this.mainApplicationClass = this.deduceMainApplicationClass();


As can be seen from the above source code, the initialization process of SpringApplication mainly includes 4 parts, the specific description is as follows.

(1) this.webApplicationType = WebApplicationType.deduceFromClasspath()

Used to determine the type of the current webApplicationType application. The deduceFromClasspath() method is used to check whether a characteristic class exists under the Classpath class path, so as to determine whether the current webApplicationType type is a SERVLET application(a traditional MVC application before Spring 5) or a REACTIVE application(WebFlux interactive application that began to appear in Spring 5)

(2) this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class))

Initializer settings for SpringApplication applications. During the initialization process, the Spring class loader SpringFactoriesLoader will be used to obtain all available application initializer classes ApplicationContextInitializer from the spring.factores file under the META-INF/spring.factories class path.

(3) this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))

Listener settings for SpringApplication applications. The process of setting the listener is basically the same as the process of setting the initializer in the previous step. It is also using SpringFactoriesLoader to get all available listener classes ApplicationListener from the spring.factores file under the META-INF in the META-INF/spring.factories class path.

(4) this.mainApplicationClass = this.deduceMainApplicationClass()

The main program startup class used to infer and set the project main() method to start