SpringBoot自动化配置原理
1. 一个注解@SpringBootApplication
1.1 @SpringBootConfiguration
底层就是一个@Configuration,表示当前引导类就是一个配置类
1.2 @ComponentScan
组件扫描,表示当前引导类所在的包及子包下的注解能够被Spring识别
1.3 @EnableAutoConfiguration
是一个组合注解,由2个注解构成
1.3.1 @AutoConfigurationPackage(了解)
底层就是一个@import注解,导入Registrar类,该类会自动去调用registerBeanDefinitions方法,该方法中能够获取到引导类所在的包,配置@ComponentScan就可以让SpringBoot去扫描引导类包下的组件
1.3.2 @Import
该注解导入一个
AutoConfigurationImportSelector
类,该类会自动调用selectImports
方法,方法内部会调用getAutoConfigurationEntry
方法,这个方法内部又会调用getCandidateConfigurations
方法,最终调用了SpringFactoriesLoader.loadFactoryNames方法,这个方法会去加载spring-boot-autoconfigure-2.6.2.jar
包下META-INF
目录下的spring.factories
的配置文件,把key为EnableAutoConfiguration的值列表加载进内存,封装成一个字符串集合,再把集合转成一个字符串数组
2. 一个方法 SpringApplication.run(引导类.class, args);
底层就是
(new SpringApplication(primarySources)).run(args)
2.1 构造器【赋值操作】
new SpringApplication(primarySources)
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = Collections.