Spring Boot automatic assembly structure diagram:

Entry point: The boot class for SpringBoot has the following contents:

@springBootApplication public class TestApplication {public static void main(String[] args) { SpringApplication. Run (TestApplication. Class, args); }}Copy the code

@ SpringBootApplication comments:

@SpringBootApplication is a composite annotation consisting of the following three annotations:

@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan

View source code:

@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 {
...
}
Copy the code

@ SpringBootConfiguration comments:

View source code:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}
Copy the code

If you look at the source code, there is an @Configuration annotation that identifies a class as Spring

For a Configuration class, the @SpringBootConfiguration annotation works just as well as the @Configuration annotation, but SpringBoot prefers the @SpringBootConfiguration annotation.

@ EnableAutoConfiguration comments:

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class<? >[] exclude() default {}; String[] excludeName() default {}; }Copy the code

@ EnableAutoConfifiguration notes and annotations, mainly contains @ AutoConfigurationPackage and

@ Import.

@ AutoConfigurationPackage comments:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({Registrar.class}) public  @interface AutoConfigurationPackage { }Copy the code

The Registrar class annotation introduces the Registrar. Class class via @import, which is mainly used to load the @SpringBootApplication annotation

In the class package structure, and store, provide a scanned package path for later Spring Boot loading resources.

@Import(AutoConfigurationImportSelector.class)

The @ Import annotations introduced a kind of AutoConfigurationImportSelector. Class, read the source code, The assembly classes from which SpringBoot is launched are read in the meta-INF/Spring.Factories file of the project in which the annotation class is launched

Spring Boot obtains the values specified by EnableAutoConfiguration from the meta-INF/Spring. factories in the classpar during startup and imports these values into the container as auto-configuration classes. The auto-configuration class does all the things we used to configure ourselves.