As Java programmers should know, every Spring Boot has a Boot class, and the Boot of Spring Boot is the main method of the class that executes. As shown below, the springApplication.run method is executed in the main method.

Start the process

Let’s summarize the Spring Boot startup process.

  • Springapplication.run is a two-step operation that encapsulates an instance of SpringApplication and then executes the overloaded run method of that instance
  • When SpringApplication wraps the instance, it reads all of the classpathMTEA-INF/spring.factoriesXML configuration file ApplicationContextInitializer initializer (containers) and ApplicaiontListener (the listener), will both wrapped in SpringApplication instances
  • Execute the Run method of the SpringApplication instance
  • The run method of the default initialization of the Annotation configuration containers AnnotationConfigApplicationContext
  • Perform the initial ApplicationContextInitializer above method
  • Then load the Bean into the container

Automatic Spring Boot configuration

As we know, some of the default configurations also take effect after importing the jar packages required for development using Maven coordinates. How does Spring Boot configure default values for these JARS? The answer is in the @SpringBootApplication annotation on SpringBoot’s Boot class.

@SpringBootApplication consists of three annotations: @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan

@ EnableAutoConfiguration bottom is composed of two annotations, @ AutoConfigurationPackage, @ Import (AutoConfigurationImportSelector. Class)

@ Import (AutoConfigurationImportSelector. Class) automatic configuration of the secret is here, the Import a lot of automatic configuration, the debug can be found, It reads auto-configuration classes in meta-INF/Spring. factories in classpath

The automatic configuration classes are as follows:

conclusion

Spring Boot reads the EnableAutoConfiguration configuration in meta-INF/Spring. factories under the classpath using @enableAutoConfiguration in @springBootApplication on the main Boot class Class, but these config classes use @conditionAlonClass and are subject to certain conditions to activate the configuration, which writes the default configuration.