Learning blog: [SpringBoot] @SpringBootApplication automatic assembly principle

The principle of automatic assembly @SpringBootApplication

pom.xml

  • spring-boot-dependencies core dependency (parent project)
  • No need to specify the version (warehouse) when introducing springboot dependencies

Launcher

<!--启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

All functional scenarios of springboot ==> corresponding launcher

main program

@SpringBootApplication  //标注 springboot应用   启动类下的所有资源被导入
public class Springboot01HelloworldApplication {
    
    

    public static void main(String[] args) {
    
    
        //通过反射加载该类的对象
        SpringApplication.run(Springboot01HelloworldApplication.class, args);
    }
}

insert image description here

Click to see and find two core annotations

@SpringBootConfiguration springboot configuration annotation
@EnableAutoConfiguration automatic configuration annotation

insert image description here


First look at @SpringBootConfiguration springboot configuration annotations

Click to find @Configuration This is a configuration class

insert image description here

Then click into @Component, which is actually a spring component

insert image description here


Look at the @EnableAutoConfiguration automatic configuration annotation

insert image description here

Discover the automatic configuration package annotation @AutoConfigurationPackage

Automatic configuration selector @Import({AutoConfigurationImportSelector.class})

Click on the automatic configuration package annotation @AutoConfigurationPackage and find that a registrar @Import({Registrar.class}) has been imported

insert image description here

Then click on the automatic configuration selector @Import({AutoConfigurationImportSelector.class}) This is the core

It defines the environment, class loader, resource loader, etc.
insert image description here

The focus is on (automatic loading of configurator entities) getAutoConfigurationEntry() method

insert image description here

Discover (obtain candidate configurations) getCandidateConfigurations() method, chase it in

insert image description here

This method obtains all loaded configuration methods SpringFactoriesLoader.loadFactoryNames(), and loads two methods. Focus on (get spring factory load factory class) getSpringFactoriesLoaderFactoryClass() method and find that it obtains the class of the automatic configuration annotation @EnableAutoConfiguration

protected Class<?> getSpringFactoriesLoaderFactoryClass() {
    
    
   return EnableAutoConfiguration.class;
}

And springboot's main startup class @SpringBootApplication

Inherited the automatic configuration annotation @EnableAutoConfiguration

insert image description here

After going around such a big circle, it does one thing, which is to import all resources under the startup class. In addition

insert image description here

If the configuration is not empty, it will go to (auto-configured core file) META-INF/spring.factories to find the configuration file

insert image description here

But the springboot automatic configuration file of version 2.7 is in

META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

insert image description here

but spring.factories can still be used and may be abolished in the future

Now look at how it loads the configuration file into SpringFactoriesLoader.loadFactoryNames

insert image description here

This method is to obtain all the configurations in the configuration file in a loop from the core configuration file META-INF/spring.factories or system files, traverse them and encapsulate them into properties for use

Note: All automatic configurations of springboot are scanned and loaded at startup, and all automatic configuration classes of spring.factories are in it. Only when the corresponding starter is imported and the conditions (@ConditionalOnXXX) are met, the automatic configuration will take effect.

Guess you like

Origin blog.csdn.net/Aurinko324/article/details/125353992