SpringBoot自動(dòng)配置的核心注解原理解析
前言
SpringBoot作為Spring生態(tài)系統(tǒng)中的重要組成部分,其最大的特色就是約定優(yōu)于配置的設(shè)計(jì)理念,而自動(dòng)配置機(jī)制正是這一理念的核心體現(xiàn)。通過自動(dòng)配置,SpringBoot能夠根據(jù)項(xiàng)目中的依賴和配置,自動(dòng)裝配相應(yīng)的Bean,極大地簡(jiǎn)化了傳統(tǒng)Spring項(xiàng)目的配置工作。
SpringBoot自動(dòng)配置概述
SpringBoot自動(dòng)配置是一套智能化的配置機(jī)制,它能夠根據(jù)classpath中存在的jar包、配置文件中的屬性值以及其他條件,自動(dòng)為應(yīng)用程序配置相應(yīng)的Bean。這種機(jī)制讓開發(fā)者無需編寫大量的XML配置文件或Java配置類,就能快速搭建一個(gè)功能完整的Spring應(yīng)用。自動(dòng)配置的核心思想是通過條件注解來判斷是否需要?jiǎng)?chuàng)建某個(gè)Bean。只有當(dāng)特定條件滿足時(shí),相應(yīng)的配置類才會(huì)生效,Bean才會(huì)被創(chuàng)建并加入到Spring容器中。
自動(dòng)配置的核心注解
@SpringBootApplication注解
SpringBoot應(yīng)用的入口點(diǎn)通常標(biāo)注有@SpringBootApplication注解,這個(gè)注解是一個(gè)復(fù)合注解,包含了三個(gè)重要的注解:

其中@EnableAutoConfiguration是自動(dòng)配置的核心注解,它負(fù)責(zé)啟動(dòng)SpringBoot的自動(dòng)配置機(jī)制。
@EnableAutoConfiguration注解解析
@EnableAutoConfiguration注解的源碼如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
這個(gè)注解通過@Import導(dǎo)入了AutoConfigurationImportSelector類,這個(gè)類是自動(dòng)配置機(jī)制的核心實(shí)現(xiàn)。
AutoConfigurationImportSelector核心實(shí)現(xiàn)
AutoConfigurationImportSelector類實(shí)現(xiàn)了ImportSelector接口,Spring容器會(huì)調(diào)用其selectImports方法來獲取需要導(dǎo)入的配置類。
selectImports方法分析
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
這個(gè)方法首先檢查自動(dòng)配置是否啟用,然后調(diào)用getAutoConfigurationEntry方法獲取自動(dòng)配置條目。
getAutoConfigurationEntry方法詳解
源碼如下:
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
這個(gè)方法的執(zhí)行流程包括以下幾個(gè)步驟:
- 獲取候選配置類列表
- 移除重復(fù)的配置類
- 獲取排除的配置類
- 檢查排除的配置類是否合法
- 從候選配置中移除排除的配置
- 通過過濾器進(jìn)一步篩選配置類
- 觸發(fā)自動(dòng)配置導(dǎo)入事件

其實(shí)主要的就是getCandidateConfigurations和filter方法,一個(gè)是來得到配置類的List集合,另一個(gè)是用來對(duì)過濾不符合條件注解的那些配置類。
getCandidateConfigurations方法
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
這個(gè)方法通過SpringFactoriesLoader.loadFactoryNames方法從META-INF/spring.factories文件中加載所有的自動(dòng)配置類。
SpringFactoriesLoader機(jī)制
SpringFactoriesLoader是SpringBoot中一個(gè)重要的工具類,它負(fù)責(zé)從classpath下的META-INF/spring.factories文件中加載配置信息。
loadFactoryNames方法實(shí)現(xiàn)
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
loadSpringFactories方法核心邏輯

通過SpringFactoriesLoader來加載META-INF/spring.factories文件中的所有自動(dòng)配置類,并將其緩存起來。
spring.factories文件格式
spring.factories文件采用Properties格式,其中key是接口或抽象類的全限定名,value是實(shí)現(xiàn)類的全限定名列表。以spring-boot-autoconfigure包中的spring.factories為例:
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ ...
條件注解機(jī)制
SpringBoot自動(dòng)配置的智能之處在于條件注解的使用。這些注解決定了配置類在什么條件下才會(huì)生效。
自動(dòng)配置的執(zhí)行流程
SpringBoot自動(dòng)配置的完整執(zhí)行流程可以概括為以下幾個(gè)階段:
- 首先是啟動(dòng)階段。當(dāng)Spring容器啟動(dòng)時(shí),會(huì)處理@SpringBootApplication注解,進(jìn)而處理@EnableAutoConfiguration注解。
- 然后是加載階段。AutoConfigurationImportSelector被調(diào)用,從META-INF/spring.factories文件中加載所有候選的自動(dòng)配置類。
- 接下來是過濾階段。通過各種條件注解對(duì)候選的配置類進(jìn)行過濾,只保留滿足條件的配置類。
- 最后是實(shí)例化階段。Spring容器實(shí)例化通過過濾的配置類,并創(chuàng)建相應(yīng)的Bean。
總結(jié)
SpringBoot的自動(dòng)配置機(jī)制通過條件注解、SpringFactoriesLoader、META-INF/spring.factories文件等技術(shù),實(shí)現(xiàn)了智能化的Bean配置。它大大簡(jiǎn)化了Spring應(yīng)用的配置工作,理解自動(dòng)配置的原理不僅有助于更好地使用SpringBoot,也能幫助我們?cè)诒匾獣r(shí)創(chuàng)建自己的自動(dòng)配置,提高開發(fā)效率。
到此這篇關(guān)于SpringBoot自動(dòng)配置的核心注解原理的文章就介紹到這了,更多相關(guān)SpringBoot自動(dòng)配置原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Javas使用Redlock實(shí)現(xiàn)分布式鎖過程解析
這篇文章主要介紹了Javas使用Redlock實(shí)現(xiàn)分布式鎖過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因
spring-boot-maven-plugin是spring boot提供的maven打包插件,本文主要介紹了SpringBoot Maven打包插件spring-boot-maven-plugin無法解析原因,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Springmvc RequestMapping請(qǐng)求實(shí)現(xiàn)方法解析
這篇文章主要介紹了Springmvc RequestMapping請(qǐng)求實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
java LocalDateTime加時(shí)間,計(jì)算兩個(gè)時(shí)間的差方式
文章介紹了如何在Java中使用LocalDateTime類添加時(shí)間并計(jì)算兩個(gè)時(shí)間的差值,通過比較來總結(jié)個(gè)人經(jīng)驗(yàn),并鼓勵(lì)讀者參考和支持腳本之家2025-03-03
SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例
這篇文章主要介紹了SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作
這篇文章主要介紹了Spring AOP使用@Aspect注解 面向切面實(shí)現(xiàn)日志橫切的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

