A preliminary study on the principle of SpringBoot

Automatic configuration:
pom.xml

  • The core dependency of spring-boot-dependences is in the parent project
  • When we write or introduce springboot dependencies, we don’t need to specify the version, because there are these version repositories
<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.3.4.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
</parent>

Launcher

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • Starter: To put it bluntly, it is the startup scene of SpringBoot
  • For example, spring-boot-starter-web, it will help us import all the dependencies of the web environment
  • Springboot turns each application scenario into a starter

Main program
@SpringBootApplication: The springboot application annotation mark on the class indicates that this class is the main configuration class of SpringBoot, and the main method of this class should be run to start the springboot program

@SpringBootApplication
public class HellosbApplication {
    
    

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

}
@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 {
    
    
  • @SpringBootConfiguration: The configuration class of SpringBoot. Marked on a certain class, indicating the configuration class of springboot

    @Configuration configuration class to mark this annotation The
    configuration class is also a component, @Component

  • @EnableAutoConfiguration: Turn on the automatic configuration function; before we need to configure ourselves, now Springboot helps us automatically configure

@AutoConfigurationPackage
@Import({
    
    AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    
    
  • @AutoConfigurationPackage: Automatic configuration package
@Import({
    
    AutoConfigurationPackages.Registrar.class})
public @interface AutoConfigurationPackage {
    
    
    String[] basePackages() default {
    
    };

    Class<?>[] basePackageClasses() default {
    
    };
}
  • @Import: Spring's bottom-level annotation, import a component into the container

There is this method in the Registrar class

 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
    
    
            AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
        }

new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames()
Return value: the name of the package where the springboot main configuration class is located

Insert picture description here

Conclusion: Scan all components in the package of the main configuration class (@SpringBootApplication) and sub-packages into the srping container

  • @Import({AutoConfigurationImportSelector.class})
    Import components to the container,
    AutoConfigurationImportSelector: Automatic configuration import selector
    This class will return all required components in the form of full class names, these components will be scanned into the container, and the container will be scanned Many configuration classes import all the components needed in this scenario to the container.

Insert picture description here

Guess you like

Origin blog.csdn.net/mmmmmlj/article/details/108918838