Understand the principle of Spring Boot automatic configuration from the three major BUFFs annotated by @SBA

picture

Is it good to use SpringBoot (SB for short) to build projects? Definitely delicious! This is all because of the male pig's foot @SBA annotation in this article, the full name is @SpringBootApplication annotation. That's right, it's the one on the startup class every time you create an SB project.

picture

For Spring, everything is a bean in the IoC container. Why is the @SBA annotation so powerful, as long as it is on the startup class, it will be done? Because he has three major BuFFs superimposed on him, he can complete the initialization of all IoC containers. Look at the ↓↓↓ picture.

picture

Behind an Internet celebrity, there are many workers who are dedicated. @SBA's comment is brilliant, but he can't do it alone. Hold down the Ctrl key and click @SBA annotation in the development tool, and look at his source code, you will find that it is composed of three annotations (the other 4 meta-annotations are not counted), these are his three major BuFFs.

These three annotations are supported by a bunch of classes in the Spring/Spring Boot framework. If you don't go into the tiger's den, you can't get a tiger's cub. Let's take a closer look at how these three BuFFs are realized.

BuFF 1: The startup class can be used to declare beans

We used to define the method in XML with Spring, but now we don't use XML, and instead add a @Configuration annotation to the class and @Bean annotation to the method. That is to say, the class is used as XML, and many beans can be declared. This is the effect of @Configuration.

Comparison of two ways of declaring beans:

XML方式:<beans>              <bean id="xx" ...>              <bean id="yy" ...>    </beans>  
类方式:@Configuration  public class XxxConfig{            @Bean public Xxx getXxx() { ... }            @Bean public Yyy getYyy() { ... }  }

@SpringBootConfiguration is the backup tire of @Configuration, so it has the same effect. It's just that @SpringBootConfiguration can also be relied upon by modules such as unit tests to find configurations. So use this long one first in the SB project.

Why can the startup class of the SB project contain multiple @Bean annotated methods as XML to declare beans? It is because of the BuFF brought by the @SpringBootConfiguration annotation.

picture

BuFF 2: Scan code and create Bean

Everyone in the SB project has a feeling that the framework will automatically scan the classes in the project code package and create them as beans, which is very convenient. This is the BuFF brought by the @ComponentScan annotation. With it, the framework scans by default the package/subpackage where the startup classes reside (recursively).

picture

Does this comment look familiar? That's right, he is the avatar of <context:component-scan> in the original Spring XML. The @ComponentScan annotation will command the two brothers AnnotatedBeanDefinitionReader and ClassPathBeanDefinitionScanner to scan beans everywhere in each code package and jar package of the project. All classes with the five annotations in the figure below will be scanned and created as beans .

BuFF 3: Automatically configure various dependent frameworks and middleware beans

This BuFF is even more powerful. Why did we just import a spring-boot-web jar package, and the SpringMVC framework took effect? Why do we import a jar package with a DataSource class (data source), the framework knows that we want to connect to the database? This is the credit of SB automatic configuration. It makes the framework look smart, and it also makes it foolish to integrate Spring with various third-party frameworks and middleware.

picture

This BuFF is brought by the @EnableAutoConfiguration annotation, which turns on the automatic import function. It will import one of the most important workers: the AutoConfigurationImportSelector class. This class is responsible for finding and loading the automatic configuration (XxxAutoConfiguration) classes listed in the META-INF/spring.factories file in each jar package. Each automatic configuration class is responsible for the creation of beans required by a certain framework and middleware .

picture

↑↑↑It is recommended to click on the picture above to enlarge it. For popular frameworks/middleware in the industry, SB supports automatic configuration integration by default. Various XxxAutoConfiguration classes are provided in the official jar package, and are listed in the META-INF/spring.factories file of the jar package.

picture

Do you want to try this kind of coquettish operation? Every programmer can develop a module by himself and make a jar package. There is a META-INF/spring.factories file in the package, write your automatic configuration class ShaBiAutoConfiguration, and others can automatically configure and integrate your modules!

Okay, let's go. In the future, the interviewer will ask you the principle of automatic configuration of the SB framework, don't say you don't understand!


If you think it's not bad, give me an affirmation from a handsome boy/girl: Scan the QR code to pay attention! share it

picture

picture

Previous wonderful (partial) articles:

Guess you like

Origin blog.csdn.net/liudun_cool/article/details/112259264