最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot自動(dòng)裝配的源碼與流程圖

 更新時(shí)間:2021年08月04日 10:14:00   作者:迷戀著你微笑的人zz  
在日常的開發(fā)過程中Spring Boot自動(dòng)裝配的特性給我們開發(fā)減少了很多重復(fù)性的工作,這篇文章主要給大家介紹了關(guān)于springboot自動(dòng)裝配的相關(guān)資料,需要的朋友可以參考下

前言

在使用SpringBoot開發(fā)項(xiàng)目中,遇到一些 XXX-XXX-starter,例如mybatis-plus-boot-starter,這些包總是能夠自動(dòng)進(jìn)行配置,
減少了開發(fā)人員配置一些項(xiàng)目配置的時(shí)間,讓開發(fā)者擁有更多的時(shí)間用于開發(fā)的任務(wù)上面。下面從源碼開始。

正文

SpringBoot版本:2.5.3

  1. 從@SpringBootApplication進(jìn)入@EnableAutoConfiguration
  2. 然后進(jìn)入AutoConfigurationImportSelector
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
            @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
    public @interface SpringBootApplication {}

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import(AutoConfigurationImportSelector.class)
    public @interface EnableAutoConfiguration {}

進(jìn)入AutoConfigurationImportSelector,可以發(fā)現(xiàn)該類是ImportSelector接口的實(shí)現(xiàn)類,然后直接定位至selectImports方法。

到了這里,其實(shí)主動(dòng)裝配的套路就是@EnableXXX加@Import的套路。這就是一個(gè)大概的認(rèn)知了。

    @Override
   public String[] selectImports(AnnotationMetadata annotationMetadata) {
      if (!isEnabled(annotationMetadata)) {
         return NO_IMPORTS;
      }
      AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
      return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
   }

下面進(jìn)入getAutoConfigurationEntry方法:

        protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
            if (!isEnabled(annotationMetadata)) {
                return EMPTY_ENTRY;
            }
            // 獲取注解信息
            AnnotationAttributes attributes = getAttributes(annotationMetadata);
            // 獲取自動(dòng)配置的信息
            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);
        }

詳細(xì)的注釋已經(jīng)寫在代碼中的了,這里面最重要的是getCandidateConfigurations方法,其次是下面的過濾排除不需要的配置信息。下面進(jìn)入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;
        }

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

        protected ClassLoader getBeanClassLoader() {
              return this.beanClassLoader;
        }

這里需要關(guān)注的方法是SpringFactoriesLoader.loadFactoryNames,進(jìn)入該方法,該方法是一個(gè)靜態(tài)方法。

   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());
   }

   private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
        // 查詢緩存
      Map<String, List<String>> result = cache.get(classLoader);
      if (result != null) {
         return result;
      }
      result = new HashMap<>();
      try {
            // 1. 獲取類加載器能讀取的所有在META-INF目錄下的spring.factories文件
         Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
         while (urls.hasMoreElements()) {
            // 遍歷路徑
            URL url = urls.nextElement();
            UrlResource resource = new UrlResource(url);
                // 將路徑下的文件數(shù)據(jù)讀取為Properties
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            // 遍歷Properties
            for (Map.Entry<?, ?> entry : properties.entrySet()) {
               String factoryTypeName = ((String) entry.getKey()).trim();
               String[] factoryImplementationNames =
                     StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
               for (String factoryImplementationName : factoryImplementationNames) {
                  result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
                        .add(factoryImplementationName.trim());
               }
            }
         }
         // Replace all lists with unmodifiable lists containing unique elements
            // 用不可修改列表替換
         result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
               .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
            // 加入緩存
         cache.put(classLoader, result);
      }
      catch (IOException ex) {
         throw new IllegalArgumentException("Unable to load factories from location [" +
               FACTORIES_RESOURCE_LOCATION + "]", ex);
      }
      return result;
   }
  1. loadSpringFactories方法就是加載并讀取所傳入的類加載器能讀取的所有spring.factories文件,并將讀取的數(shù)據(jù)最終轉(zhuǎn)換為Map<String, List>類型的數(shù)據(jù),然后存入本地緩存。
  2. loadFactoryNames方法就是通過傳入factoryType(也就是calss.name)來獲取數(shù)據(jù),并不是獲取所有的數(shù)據(jù)。所以這里會(huì)有很多地方會(huì)用到。
  3. @EnableAutoConfiguration是取的文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration,所以一般自定義starter的自動(dòng)配置文件都是在這個(gè)key后面。例如:org.springframework.boot.autoconfigure.EnableAutoConfiguration= a.b.c.d.XXX;

至此,源碼就差不多完成了,其實(shí)從源碼上來看其實(shí)也不難。

大概流程圖如下:

最后

說了這么多源碼,也畫了流程圖。這里面最重要的就是SpringFactoriesLoader,從這個(gè)類名就可以看出來,是專門處理factories文件的,這個(gè)類只提供了兩個(gè)靜態(tài)方法,而EnableAutoConfiguration只是取了其中的EnableAutoConfiguration下的數(shù)據(jù),那么其它的數(shù)據(jù)呢,不會(huì)用到嗎?肯定不會(huì)的,所以spring在很多地方會(huì)用到這個(gè)類,后面看spring源碼的時(shí)候在來看吧,這里先標(biāo)記一下。

還有就是,SpringFactoriesLoader和JDK的SPI也是差不多的一個(gè)思想。下一篇就來看看JDK的SPI

到此這篇關(guān)于springboot自動(dòng)裝配的源碼與流程圖的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

苍溪县| 陵水| 塔城市| 恭城| 剑阁县| 南漳县| 河曲县| 栾川县| 禄丰县| 水富县| 乌什县| 清水河县| 巨野县| 卢氏县| 日照市| 页游| SHOW| 云和县| 嘉鱼县| 新野县| 洱源县| 博爱县| 云和县| 育儿| 大埔区| 申扎县| 开平市| 嵊泗县| 准格尔旗| 西乡县| 炎陵县| 曲周县| 宁乡县| 永善县| 靖江市| 石楼县| 英德市| 昌平区| 华宁县| 当阳市| 眉山市|