問(wèn)題:我們并未寫(xiě)類似kafka的配置類,SpringBoot是如何導(dǎo)入配置的呢?
- 打開(kāi)
@SpringBootApplication可看到@EnableAutoConfiguration,即開(kāi)啟自動(dòng)配置功能 - 打開(kāi)
@EnableAutoConfiguration可看到@Import(AutoConfigurationImportSelector.class),即導(dǎo)入了AutoConfigurationImportSelector.class類,自動(dòng)配置導(dǎo)入選擇器。 - 再看
selectImports方法,
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
再進(jìn)入getCandidateConfigurations()方法,
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
再進(jìn)入loadFactoryNames()方法,
loadSpringFactories(classLoader).getOrDefault(factoryClassName,
Collections.emptyList());
再進(jìn)入loadSpringFactories()方法,
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
可看到
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
在spring-boot-autoconfigure-2.0.4.RELEASE.jar!/META-INF/spring.factories可看到,里面有springboot模塊的自動(dòng)配置文件的全目錄

-
答:SpringBoot在
spring-boot-autoconfigure-2.0.4.RELEASE.jar里面準(zhǔn)備好了所有的配置類,在啟動(dòng)的時(shí)候,可以自動(dòng)導(dǎo)入所有配置。以前需要自己手動(dòng)寫(xiě)的配置類就不用寫(xiě)了(有自動(dòng)配置類沒(méi)有的配置還是要自定義)。