SpringBoot 自動(dòng)配置

問(wèn)題:我們并未寫(xiě)類似kafka的配置類,SpringBoot是如何導(dǎo)入配置的呢?

  1. 打開(kāi)@SpringBootApplication可看到@EnableAutoConfiguration,即開(kāi)啟自動(dòng)配置功能
  2. 打開(kāi)@EnableAutoConfiguration可看到@Import(AutoConfigurationImportSelector.class),即導(dǎo)入了AutoConfigurationImportSelector.class類,自動(dòng)配置導(dǎo)入選擇器。
  3. 再看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)配置文件的全目錄

  1. :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)有的配置還是要自定義)。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容