SpringBoot自動(dòng)配置原理,你真的懂嗎?

概述

上面博文(SpringBoot簡(jiǎn)介與快速搭建)我們簡(jiǎn)單的介紹了什么是SpringBoot,以及如何使用SpringBoot,但是我們對(duì)于SpringBoot的基本原理并沒(méi)有介紹,這篇博文我們重點(diǎn)介紹SpringBoot是如何實(shí)現(xiàn)的自動(dòng)配置。


依賴管理

在我們的pom文件中最核心的依賴就一個(gè):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4</version>
    <relativePath/>
</parent>

它的父項(xiàng)目依賴,規(guī)定所有依賴的版本信息:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.4.4</version>
</parent>

由此,我們發(fā)現(xiàn)springboot框架幾乎聲明了所有開(kāi)發(fā)中常用的依賴的版本號(hào),無(wú)需關(guān)注版本號(hào),而且實(shí)現(xiàn)了自動(dòng)版本仲裁機(jī)制,當(dāng)然了我們也可以根據(jù)我們的需要,替換掉默認(rèn)的依賴版本。


核心注解@SpringBootApplication

@SpringBootApplication
public class BootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
}

在上面的啟動(dòng)類中我們發(fā)現(xiàn)了一個(gè)陌生的注解@SpringBootApplication,這個(gè)注解的是什么含義呢?我們點(diǎn)進(jìn)去看一下。

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

其實(shí)@SpringBootApplication是上面三個(gè)注解的組合體,我們對(duì)這三個(gè)注解理解清楚就可以了,下面逐個(gè)進(jìn)行解釋:


@SpringBootConfiguration

@Configuration
public @interface SpringBootConfiguration {

@Configuration我們并不陌生,它允許在上下文中注冊(cè)額外的bean或?qū)肫渌渲妙悾珸SpringBootConfiguration其實(shí)代表當(dāng)前類是一個(gè)配置類。


@EnableAutoConfiguration

EnableAutoConfiguration的目的是啟動(dòng)SpringBoot的自動(dòng)配置機(jī)制。

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

1、AutoConfigurationPackage指定默認(rèn)的包規(guī)則

@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

AutoConfigurationPackage注解的作用是將 添加該注解的類所在的package 作為 自動(dòng)配置package 進(jìn)行管理。也就是說(shuō)當(dāng)SpringBoot應(yīng)用啟動(dòng)時(shí)默認(rèn)會(huì)將啟動(dòng)類所在的package作為自動(dòng)配置的package。然后使用@Import注解將其注入到ioc容器中。這樣,可以在容器中拿到該路徑。

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {

   @Override
   public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
      register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));
   }

   @Override
   public Set<Object> determineImports(AnnotationMetadata metadata) {
      return Collections.singleton(new PackageImports(metadata));
   }

}

重點(diǎn)看下registerBeanDefinitions方法。

方法的第二個(gè)參數(shù)通過(guò)new PackageImport(metadata).getPackageName()方法設(shè)置。

接著看下PackageImport的構(gòu)造器方法。

PackageImports(AnnotationMetadata metadata) {
   AnnotationAttributes attributes = AnnotationAttributes
         .fromMap(metadata.getAnnotationAttributes(AutoConfigurationPackage.class.getName(), false));
   List<String> packageNames = new ArrayList<>(Arrays.asList(attributes.getStringArray("basePackages")));
   for (Class<?> basePackageClass : attributes.getClassArray("basePackageClasses")) {
      packageNames.add(basePackageClass.getPackage().getName());
   }
   if (packageNames.isEmpty()) {
      packageNames.add(ClassUtils.getPackageName(metadata.getClassName()));
   }
   this.packageNames = Collections.unmodifiableList(packageNames);
}

ClassUtils.getPackageName(metadata.getClassName())獲取標(biāo)注@AutoConfigurationPackage注解的類的全限定名。

最后,利用Registrar給容器中導(dǎo)入一系列組件,將指定的包下的所有組件導(dǎo)入進(jìn)來(lái)。

2、@Import(AutoConfigurationImportSelector.class)

使用Import自動(dòng)導(dǎo)入所有符合自動(dòng)配置條件的Bean定義并加載到IOC容器

@Override
        public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
            Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,
                    () -> String.format("Only %s implementations are supported, got %s",
                            AutoConfigurationImportSelector.class.getSimpleName(),
                            deferredImportSelector.getClass().getName()));
            AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
                    .getAutoConfigurationEntry(annotationMetadata);
            this.autoConfigurationEntries.add(autoConfigurationEntry);
            for (String importClassName : autoConfigurationEntry.getConfigurations()) {
                this.entries.putIfAbsent(importClassName, annotationMetadata);
            }
        }

1、利用getAutoConfigurationEntry(annotationMetadata);給容器中批量導(dǎo)入一些組件
2、調(diào)用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)獲取到所有需要導(dǎo)入到容器中的配置類
3、利用工廠加載 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的組件
4、從META-INF/spring.factories位置來(lái)加載一個(gè)文件。
默認(rèn)掃描我們當(dāng)前系統(tǒng)里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.4.4.RELEASE.jar包里面也有META-INF/spring.factories

在這里插入圖片描述

文件里面寫(xiě)死了spring-boot一啟動(dòng)就要給容器中加載的所有配置類spring-boot-autoconfigure-2.4.4.RELEASE.jar/META-INF/spring.factories,一共130個(gè)自動(dòng)配置類。

130個(gè)場(chǎng)景的所有自動(dòng)配置,會(huì)在springboot啟動(dòng)的時(shí)候默認(rèn)全部加載。xxxxAutoConfiguration會(huì)按照條件裝配規(guī)則(@Conditional),最終會(huì)按需配置。

小結(jié):

SpringBoot為我們的應(yīng)用程序啟用了三個(gè)功能:自動(dòng)配置,組件掃描,以及能夠在"應(yīng)用類"上定義額外的配置。


@ComponentScan

@Component在應(yīng)用程序所在的軟件包上啟用掃描,指定掃描哪些Spring注解。


ServletWebServerFactoryAutoConfiguration為例

在130個(gè)場(chǎng)景有我們比較熟悉兩個(gè)組件,ServletWebServerFactoryAutoConfiguration和WebMvcAutoConfiguration,我們以ServletWebServerFactoryAutoConfiguration為例,看一下SpringBoot是如何自動(dòng)裝配的webServer。

在這里插入圖片描述

在注解中我們看到了大量以@Conditional開(kāi)頭的注解,即條件裝配,滿足Conditional指定的條件,則進(jìn)行組件注入。@EnableConfigurationProperties(ServerProperties.class)+@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true),讀取我們?cè)谂渲梦募帉?xiě)的屬性,并把它封裝到JavaBean中,以供隨時(shí)使用。

此時(shí)我們的Tomcat容器已經(jīng)以Bean的形式被注入到了IOC容器中。


如何禁用特定的自動(dòng)配置類

如果發(fā)現(xiàn)應(yīng)用中不需要特定自動(dòng)配置類,則可以使用exclude屬性@SpringBootApplication來(lái)禁用它們,如以下示例所示:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
//@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"})
public class MyApplication {
}

如果該類不在類路徑中,則可以使用excludeName注釋的屬性,并指定完全限定的名稱(全類名字符串)。定義排除項(xiàng),即可以使用哪個(gè)注釋級(jí)別也可以使用屬性來(lái)定義。


總結(jié)

  • SpringBoot預(yù)先加載META-INF/spring.factories中所有的自動(dòng)配置類,xxxxxAutoConfiguration

  • 每個(gè)自動(dòng)配置類按照條件進(jìn)行生效,默認(rèn)都會(huì)綁定配置文件指定的值。xxxxProperties里面拿。xxxProperties和配置文件進(jìn)行了綁定

  • 生效的配置類就會(huì)給容器中裝配很多組件,只要容器中有這些組件,相當(dāng)于有了這些功能

  • 定制化配置

    • 用戶直接自己@Bean替換底層的組件
    • 用戶根據(jù)這個(gè)組件是獲取的配置文件的什么值,可以自行修改。

EnableAutoConfiguration ---> 掃描xxxxxAutoConfiguration ---> 根據(jù)條件@Conditional裝配組件 --->根據(jù)xxxxProperties加載屬性值 ----> application.properties


作者:程序猿小亮

博主寫(xiě)作不易,加個(gè)關(guān)注唄

求關(guān)注、求點(diǎn)贊,加個(gè)關(guān)注不迷路,感謝

點(diǎn)贊是對(duì)我最大的鼓勵(lì)
↓↓↓↓↓↓

最后編輯于
?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

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