App下載

詳解SpringBoot自動裝配原理

猿友 2021-07-15 13:50:55 瀏覽數(shù) (1812)
反饋

運(yùn)行原理

為了研究,我們正常從父項目的pom.xml開始進(jìn)行研究。

pom.xml

父依賴 spring-boot-starter-parent主要用來管理項目的資源過濾和插件


<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

點父依賴進(jìn)去查看,發(fā)現(xiàn)還有一個父依賴spring-boot-dependencies,這里的這個父依賴才是真正管理springboot應(yīng)用里面的所有依賴版本的地方,是springboot的版本控制中心。

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

啟動器:spring-boot-starter-xxx:springboot的場景啟動器

spring-boot-starter-web:導(dǎo)入web依賴的組件


<dependency>    <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

主程序

@SpringBootApplication

作用:標(biāo)注這是一個springboot主程序類,說明這是一個springboot應(yīng)用,springboot就是運(yùn)行這個類的mian方法啟動的springboot應(yīng)用。


@SpringBootApplication //標(biāo)注這是一個主程序類,說明這是一個springboot應(yīng)用
public class Springboot01HelloworldApplication {

  public static void main(String[] args) {
    //這里啟動了一個服務(wù),而不是執(zhí)行了一個方法。
    SpringApplication.run(Springboot01HelloworldApplication.class, args);
  }
}

點@SpringBootApplication繼續(xù)研究,會發(fā)現(xiàn)有@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan這三個注解

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

1.@ComponentScan: spring自動掃描包

這個我們在spring配置文件中見到過,它用來自動掃描并加載符合條件的組件或者bean,并將bean加載到IOC容器中。

2.@SpringBootConfiguration: springboot的配置類

標(biāo)注在某個類上,說明這個類是springboot的配置類,在這里它就說明SpringBootApplication這個類是springboot的配置類。

我們繼續(xù)點@SpringBootConfiguration進(jìn)去查看,會發(fā)現(xiàn) @Configuration這個注解

2.1 @Configuration:配置類,用來配置spring的xml文件

我們繼續(xù)點@Configuration進(jìn)去查看,會發(fā)現(xiàn) @Component這個注解。

2.2 @Component:組件,說明啟動類本身也是一個組件,負(fù)責(zé)啟動應(yīng)用。

至此,@SpringBootConfiguration這條線,我們研究完了。

3.@EnableAutoConfiguration:開啟自動裝配,通過@EnableAutoConfiguration來幫我們自動配置之前我們需要配置的東西。
我們繼續(xù)點@EnableAutoConfiguration進(jìn)去查看,會發(fā)現(xiàn) @AutoConfigurationPackage和@Import({AutoConfigurationImportSelector.class}) 這兩個注解。


@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 {};
}

3.1 @AutoConfigurationPackage自動裝配包

繼續(xù)點進(jìn)去查看,出現(xiàn)@Import({Registrar.class})這個注解

3.1.1 @Import({Registrar.class}): spring底層注解,給容器導(dǎo)入一個組件

Registrar.class: 將主啟動類所在包及所在包下面的所有子包里面所有的組件都掃描到Spring容器。

至此,@AutoConfigurationPackage這條線我們也研究完了。

3.2 @Import({AutoConfigurationImportSelector.class}): 給容器導(dǎo)入組件

AutoConfigurationImportSelector.class:自動裝配導(dǎo)入選擇器。

導(dǎo)入的選擇器分析:

1.我們點進(jìn)去AutoConfigurationImportSelector.class這個類的源碼進(jìn)行探究,

AutoConfigurationImportSelector.class

2.我們點擊getCandidateConfigurations進(jìn)一步分析

  protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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;
  }

2.1 使用了getSpringFactoriesLoaderFactoryClass()方法,返回一開始我們看到的啟動自動配置文件的注解類EnableAutoConfiguration.class

  protected Class<?> getSpringFactoriesLoaderFactoryClass() {
    return EnableAutoConfiguration.class;
  }

2.2 發(fā)現(xiàn)它調(diào)用了SpringFactoriesLoader類的靜態(tài)方法,我們點擊loadFactoryNames進(jìn)入loadFactoryNames()

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ClassLoader classLoaderToUse = classLoader;
    if (classLoader == null) {
      classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
    }

    String factoryTypeName = factoryType.getName();
    return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
  }

發(fā)現(xiàn)它又調(diào)用了loadSpringFactories()方法,點進(jìn)去查看

 private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
    Map<String, List<String>> result = (Map)cache.get(classLoader);
    if (result != null) {
      return result;
    } else {
      HashMap result = new HashMap();

      try {
        Enumeration urls = classLoader.getResources("META-INF/spring.factories");

        while(urls.hasMoreElements()) {
          URL url = (URL)urls.nextElement();
          UrlResource resource = new UrlResource(url);
          Properties properties = PropertiesLoaderUtils.loadProperties(resource);
          Iterator var6 = properties.entrySet().iterator();

          while(var6.hasNext()) {
            Entry<?, ?> entry = (Entry)var6.next();
            String factoryTypeName = ((String)entry.getKey()).trim();
            String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
            String[] var10 = factoryImplementationNames;
            int var11 = factoryImplementationNames.length;

            for(int var12 = 0; var12 < var11; ++var12) {
              String factoryImplementationName = var10[var12];
              ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
                return new ArrayList();
              })).add(factoryImplementationName.trim());
            }
          }
        }

        result.replaceAll((factoryType, implementations) -> {
          return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
        });
        cache.put(classLoader, result);
        return result;
      } catch (IOException var14) {
        throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
      }
    }
  }

源碼分析:

  1. MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);獲得classLoader,我們返回可以看到這里得到的就是EnableAutoConfiguration標(biāo)注的類本身
  2. Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");獲取一個資源 "META-INF/spring.factories"
  3. while循環(huán),讀取到的資源遍歷,封裝成為一個Properties

spring.factories文件

spring.factories文件

WebMvcAutoConfiguration

我們在上面的自動配置類隨便找一個打開看看,比如 :WebMvcAutoConfiguration

WebMvcAutoConfiguration

都是大家熟悉的配置,所以,自動配置真正實現(xiàn)是從classpath中搜尋所有的META-INF/spring.factories配置文件 ,并將其中對應(yīng)的 org.springframework.boot.autoconfigure. 包下的配置項,通過反射實例化為對應(yīng)標(biāo)注了 @Configuration的JavaConfig形式的IOC容器配置類 , 然后將這些都匯總成為一個實例并加載到IOC容器中。

總結(jié)

  1. SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值
  2. 將這些值作為自動配置類導(dǎo)入容器,自動配置類就生效,幫我們進(jìn)行自動配置工作;
  3. 整個J2EE的整體解決方案和自動配置都在springboot-autoconfigure的jar包中;
  4. 它會給容器中導(dǎo)入非常多的自動配置類 (xxxAutoConfiguration), 就是給容器中導(dǎo)入這個場景需要的所有組件 , 并配置好這些組件 ;
  5. 有了自動配置類 , 免去了我們手動編寫配置注入功能組件等的工作;

主啟動類

SpringApplication

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

分析:

  1. SpringbootApplication.class:應(yīng)用參數(shù)的入口
  2. args:命令行參數(shù)
  3. 該方法返回的是一個ConfigurableApplicationContext對象

SpringApplication主要做的事情:

  1. 推斷應(yīng)用的類型是普通的項目還是Web項目
  2. 查找并加載所有可用初始化器 , 設(shè)置到initializers屬性中
  3. 找出所有的應(yīng)用程序監(jiān)聽器,設(shè)置到listeners屬性中
  4. 推斷并設(shè)置main方法的定義類,找到運(yùn)行的主類

以上就是關(guān)于SpringBoot自動裝配原理的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot框架的資料請關(guān)注W3Cschool其它相關(guān)文章!


0 人點贊