Spring Boot is a spring-projects agile development Framework based on the Spring Framework. With Spring Boot we can quickly build standalone production-grade applications. As for why you can build applications quickly, one of Spring Boot’s features, the autowage and third-party Starter libraries, should be mentioned.

All the code and descriptions in this article are based on Spring Boot 2.2.5 RELEASE. Other versions of Spring Boot may differ somewhat from this article

@SpringBootApplication

One of the main features of SpringBoot applications is that you can start a project using the standard main(String[] args) method. The Boot class is annotated with the @SpringBootApplication annotation, which is the core annotation of SpringBoot applications. Everything starts with it.

Let’s first look at the internal structure of the @SpringBootApplication annotation:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(...).public @interface SpringBootApplication {
    ...
}
Copy the code

I’ve omitted some of the things we don’t need to focus on at this point, focusing on @SpringBootConfiguration, @ComponentScan, and @EnableAutoConfiguration. The @SpringBootConfiguration meta-annotation only has @Configuration, which marks our application startup class as the application Configuration class, and nothing else. The @ComponentScan annotation function is similar to the < Context: Component-scan > tag of the Spring FrameworkXML configuration era. When basePackage is not specified, the default scanning path is the package of the annotated class. Since the main focus of this article is on the autoload principle, the other two annotations are covered in the previous one, and the main observation object is @enableAutoConfiguration.

@EnableAutoConfiguration

In the actual application development process, projects usually need to rely on a lot of third-party class libraries, such as ORM framework Mybatis, Hibernate, template framework Thymeleaf, Freemarker, micro-services related Spring Cloud Eureka, Spring Cloud Gateway and so on. In Spring Boot applications, we usually only need to introduce the relevant library Starter into the POM file, almost out of the box. Behind the scenes, Spring Boot’s autowiring features work.

Let’s take a look at the annotation structure of @enableAutoConfiguration

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ...
}
Copy the code

At present, we only know that adding this annotation can activate the autowiring feature of the Spring Boot application, but the exact autowiring process is not known. It doesn’t matter, now we only need to remember @ Import (AutoConfigurationImportSelector. Class) this line yuan annotations, as for the specific do use after we speak slowly.

When it comes to the implementation of autowiring, we have to start from the application startup process of Spring Boot.

  • About the Spring Boot application startup, I roughly divide it into the following stages:
    • Spring application initialization phase, in which the Spring application completes:
      • Apply type inference
      • Load all meta-INF /spring.factories file data from the classpath and cache it (important)
      • Get the initializer, or listener, from the cache to load
      • Apply primary class (prime class) inference
    • Spring application startup preparation phase, in the current phase the Spring application will complete:
      • The application listener is fetched and loaded from the cache, and the startup event is triggered
      • Encapsulate application startup parameters and system environment variables
      • Get and load the Spring Boot application exception reporter from the cache
      • Initializes the application context based on the application type inferred during initialization
      • Refreshing (starting) the Spring application context begins a major part of Spring application preparation
    • The Spring application startup phase, during which the Spring application completes:
      • Preprocessing Spring applications (changing application status flags, startup timing, etc.)
      • rightBeanFactoryRegisters a series of built-in beans
      • Execute application context pairsBeanFactoryThe post-processing operation of
      • Perform BeanFactory post-processing operations for Spring Bean parsing, registration (emphasis)
      • After that, it is time to register the Bean handler, initialize the message source, initialize the event broadcaster, perform the application context additional startup logic, register the listener, and finishBean FactoryInitialize the logic and invoke the Spring Bean’s post-processing logic to complete the startup phase of the application and deliver the Bean toLifecycle ProcesserConduct life cycle management
      • The last callCommandLineRunnerandApplicationRunnerImplement the custom logic of the class, complete the application startup process, and enter the application running stage

summary

In this article we’ve looked at the three meta-annotations of @SpringBootApplication and the coarse-grained startup process of SpringBoot applications. You’ll notice that I’ve highlighted two important points in bold during the startup process. In the next article, we’ll focus on these two points. The automatic assembly process is discussed in detail.

Here is ShawJie, I will try to update some of my recent reading or want to write regularly, also welcome to my blog shawjie. me