How does SpringBoot realize automatic assembly?

1 How does SpringBoot automatically assemble and how to start the built-in Tomcat to run.

1.1 SpringBootApplication annotation and composition

@SpringBootApplication
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }
}

The above code calls the static run method of SpringApplication, runs a SpringApplication instance with the default configuration, and returns a running Spring context. Among them, the default configuration is loaded through the @SpringBootApplication annotation, which means that the automatic assembly of SpringBoot is completed through this 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 {
    
    

@SpringBootApplication is a composite annotation, used to quickly configure the startup class. There are 7 annotations on the annotations, of which the first 4 are meta annotations, which are used to modify the current annotations and have no actual function. There are three important notes.

  • @ComponentScan: Tell Spring to automatically scan the classes that use @Service, @Component, @Repository, and @Controller in the package, and register them as beans.

  • @SpringBootConfiguration: Identified by @Configuration, declares that the current class is a configuration class, which is equivalent to the xml file of Spring configuration.

  • @EnableAutoConfiguration: The function is to enable automatic assembly, so that Spring Boot will automatically set the current project according to the jar package dependencies in the classpath. For example, if Spring-boot-starter-web dependency is added, Tomcat and SpringMVC dependencies will be automatically added, and both will be automatically configured at startup.

SpringBoot only scans the same-level packages and subpackages of the class where the @SpringBootApplication annotation is located.

1.2 @EnableAutoConfiguration automatic assembly

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration

@EnableAutoConfiguration will import the default configuration of the AutoConfigurationImportSelector selector

// AutoConfigurationImportSelector
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
    
    
   if (!isEnabled(annotationMetadata)) {
    
    
      return NO_IMPORTS;
   }
   // 核心加载语句
   AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
   return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
    
    
    if (!isEnabled(annotationMetadata)) {
    
    
        return EMPTY_ENTRY;
    }
    AnnotationAttributes attributes = getAttributes(annotationMetadata);
    List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
    ....
}

// 返回自动配置的类名
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    
    
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
                                                                         getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
                    + "are using a custom packaging, make sure that file is correct.");
    return configurations;
}

Through code tracing, we can learn that @EnableAutoConfiguration annotation mainly loads the automatic configuration classes in ETA-INF/spring.factories. In spring.factories, we can find classes related to Tomcat and SpringMVC.

// 内置servlet/Reactive服务器的相关配置
// 如果使用到Tomcat,则会加载内联Tomcat配置(Jetty/Netty同理)
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
// 内部会加载Tomcat
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

Guess you like

Origin blog.csdn.net/LIZHONGPING00/article/details/112689978