Automatic assembly of springboot


1. What is springboot?

Springboot is essentially spring, but it does not require cumbersome configuration like spring, which simplifies the development process, but it does not provide the core functions of spring, it is just a scaffolding of spring.

By introducing dependencies (Starter), the corresponding classes that need to be automatically assembled are obtained from the spring.factories file, and corresponding Bean objects are generated, and then they are handed over to the spring container for us to manage. This is the automatic assembly of springboot .

2. What is the significance of the spring.factories file

At the entrance of our main program, about the @SpringBootApplication annotation:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
    
     @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    
    

@ComponentScanAnnotation refers to scanning @SpringBootApplicationall annotated beans under the basepackage where the annotated entry program class is located @Component, and injecting them into the container.

But if it is a jar package that is added to the maven coordinate dependency, it is a Bean outside the project root directory. @EnableAutoConfigurationAnnotations to register beans outside the project package. The spring.factories file is used to record the bean class names that need to be registered outside the project package.

3. The process of springboot automatic assembly

When springboot starts, an **Applicationobject will be created, and some parameters will be initialized in the object's construction method. The most important thing is to judge the type of the current application and set the initializer and listener, and in this process, spring will be loaded . The factories file, when an AutoConfiguration class satisfies @Conditionalthe validity conditions specified by its annotations (dependencies provided by Starters, configuration or whether a Bean exists in the Spring container, etc.), instantiate the Bean defined in the AutoConfiguration class and inject it into the Spring container, The automatic configuration of the dependent framework can be completed.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44153131/article/details/129678752