欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring FactoriesLoader機(jī)制實(shí)例詳解

 更新時間:2020年03月16日 14:18:03   作者:markLogZhu  
這篇文章主要介紹了Spring FactoriesLoader機(jī)制實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一、SpringFactoriesLoader 介紹

1.1 SpringFactoriesLoader 簡介

SpringFactoriesLoader 工廠加載機(jī)制是 Spring 內(nèi)部提供的一個約定俗成的加載方式,與 java spi 類似,只需要在模塊的 META-INF/spring.factories 文件中,以 Properties 類型(即 key-value 形式)配置,就可以將相應(yīng)的實(shí)現(xiàn)類注入 Spirng 容器中。

Properties 類型格式:

key:是全限定名(抽象類|接口)

value:是實(shí)現(xiàn),多個實(shí)現(xiàn)通過 **逗號** 進(jìn)行分隔

1.2 SpringFactoriesLoader 常用方法

loadFactoryNames

讀取 classpath上 所有的 jar 包中的所有 META-INF/spring.factories屬 性文件,找出其中定義的匹配類型 factoryClass 的工廠類,然后并返回這些工廠類的名字列表,注意是包含包名的全限定名。
loadFactories

讀取 classpath 上所有的jar包中的所有 META-INF/spring.factories 屬性文件,找出其中定義的匹配類型 factoryClass 的工廠類,然后創(chuàng)建每個工廠類的對象/實(shí)例,并返回這些工廠類對象/實(shí)例的列表。

1.3 loadFactories 流程圖

二、SpringFactoriesLoader 源碼解析

2.1 loadFactoryNames 解析

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
  // 獲取包含包名的工廠類名稱
  String factoryTypeName = factoryType.getName();
  // 獲取所有配置在 META-INF/spring.factories 文件的值
  // 然后獲取指定類的實(shí)現(xiàn)類名列表
  return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
// 默認(rèn)的工廠配置路徑地址,可以存放在多個 JAR 包下
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
  // 判斷是否有緩存結(jié)果,如果有直接返回
  MultiValueMap<String, String> result = cache.get(classLoader);
  if (result != null) {
    return result;
  }

  try {
    // 掃描 classpath 上所有 JAR 中的文件 META-INF/spring.factories
    Enumeration<URL> urls = (classLoader != null ?
        classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
        ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
    result = new LinkedMultiValueMap<>();
    while (urls.hasMoreElements()) {
      // 找到的每個 META-INF/spring.factories 文件都是一個 Properties 文件,將其內(nèi)容加載到一個 Properties 對象然后處理其中的每個屬性
      URL url = urls.nextElement();
      UrlResource resource = new UrlResource(url);
      Properties properties = PropertiesLoaderUtils.loadProperties(resource);
      for (Map.Entry<?, ?> entry : properties.entrySet()) {
        // 獲取工廠類名稱(接口或者抽象類的全限定名)
        String factoryTypeName = ((String) entry.getKey()).trim();
        // 將逗號分割的屬性值逐個取出,然后放到 結(jié)果result 中去
        for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
          result.add(factoryTypeName, factoryImplementationName.trim());
        }
      }
    }
    // 將結(jié)果存放到緩存中
    cache.put(classLoader, result);
    return result;
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Unable to load factories from location [" +
        FACTORIES_RESOURCE_LOCATION + "]", ex);
  }
}
default V getOrDefault(Object key, V defaultValue) {
  V v;
  return (((v = get(key)) != null) || containsKey(key))
    ? v
    : defaultValue;
}

2.2 loadFactories 解析

public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader) {
  Assert.notNull(factoryType, "'factoryType' must not be null");
  // 如果未指定類加載器,則使用默認(rèn)的
  ClassLoader classLoaderToUse = classLoader;
  if (classLoaderToUse == null) {
    classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
  }
  // 獲取指定工廠名稱列表
  List<String> factoryImplementationNames = loadFactoryNames(factoryType, classLoaderToUse);
  // 如果記錄器Trace跟蹤激活的話,將工廠名稱列表輸出
  if (logger.isTraceEnabled()) {
    logger.trace("Loaded [" + factoryType.getName() + "] names: " + factoryImplementationNames);
  }
  // 創(chuàng)建結(jié)果集
  List<T> result = new ArrayList<>(factoryImplementationNames.size());
  for (String factoryImplementationName : factoryImplementationNames) {
    // 實(shí)例化工廠類,并添加到結(jié)果集中
    result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse));
  }
  // 對結(jié)果集列表進(jìn)行排序
  AnnotationAwareOrderComparator.sort(result);
  return result;
}
private static <T> T instantiateFactory(String factoryImplementationName, Class<T> factoryType, ClassLoader classLoader) {
  try {
    Class<?> factoryImplementationClass = ClassUtils.forName(factoryImplementationName, classLoader);
    if (!factoryType.isAssignableFrom(factoryImplementationClass)) {
      throw new IllegalArgumentException(
          "Class [" + factoryImplementationName + "] is not assignable to factory type [" + factoryType.getName() + "]");
    }
    return (T) ReflectionUtils.accessibleConstructor(factoryImplementationClass).newInstance();
  }
  catch (Throwable ex) {
    throw new IllegalArgumentException(
      "Unable to instantiate factory class [" + factoryImplementationName + "] for factory type [" + factoryType.getName() + "]",
      ex);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • MyBatis與MyBatis-Plus的區(qū)別詳解

    MyBatis與MyBatis-Plus的區(qū)別詳解

    本文主要介紹了MyBatis與MyBatis-Plus的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java線程間的通信方式詳解

    Java線程間的通信方式詳解

    這篇文章主要為大家詳細(xì)介紹了Java線程間的通信方式,以代碼結(jié)合文字的方式來討論線程間的通信,感興趣的朋友可以參考一下
    2016-05-05
  • SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪問

    SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪問

    對于數(shù)據(jù)訪問層,無論是SQL還是NOSQL,Spring Boot默認(rèn)采用整合Spring Data的方式進(jìn)行統(tǒng)一處理,添加大量自動配置,屏蔽了很多設(shè)置,下面這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)記錄之?dāng)?shù)據(jù)訪問,需要的朋友可以參考下
    2022-04-04
  • java對ArrayList排序代碼示例

    java對ArrayList排序代碼示例

    本文通過代碼示例給大家介紹java對arraylist排序,代碼簡潔易懂,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • 使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法

    使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法

    這篇文章主要介紹了使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • java實(shí)現(xiàn)撲克牌分發(fā)功能

    java實(shí)現(xiàn)撲克牌分發(fā)功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)撲克牌分發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java實(shí)戰(zhàn)之制作在線音樂網(wǎng)站

    Java實(shí)戰(zhàn)之制作在線音樂網(wǎng)站

    這篇文章主要介紹了如何通過Java實(shí)現(xiàn)一個精美風(fēng)的在線音樂網(wǎng)站,文章采用到了JSP、JQuery、Ajax等技術(shù),感興趣的小伙伴可以了解一下
    2022-02-02
  • spring Roo安裝使用簡介

    spring Roo安裝使用簡介

    這篇文章主要介紹了spring Roo安裝使用簡介,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • SpringBoot配置攔截器的示例

    SpringBoot配置攔截器的示例

    這篇文章主要介紹了SpringBoot配置攔截器的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • maven配置多個鏡像的實(shí)現(xiàn)方法

    maven配置多個鏡像的實(shí)現(xiàn)方法

    這篇文章主要介紹了maven配置多個鏡像的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評論