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);
}
}
Click to see and find two core annotations
@SpringBootConfiguration springboot configuration annotation
@EnableAutoConfiguration automatic configuration annotation
First look at @SpringBootConfiguration springboot configuration annotations
Click to find @Configuration This is a configuration class
Then click into @Component, which is actually a spring component
Look at the @EnableAutoConfiguration automatic configuration annotation
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
Then click on the automatic configuration selector @Import({AutoConfigurationImportSelector.class}) This is the core
It defines the environment, class loader, resource loader, etc.
The focus is on (automatic loading of configurator entities) getAutoConfigurationEntry() method
Discover (obtain candidate configurations) getCandidateConfigurations() method, chase it in
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
After going around such a big circle, it does one thing, which is to import all resources under the startup class. In addition
If the configuration is not empty, it will go to (auto-configured core file) META-INF/spring.factories to find the configuration file
But the springboot automatic configuration file of version 2.7 is in
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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
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.