2022.5.6

  1. private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
  2. Map<String, List<String>> result = (Map)cache.get(classLoader);
  3. if (result != null) {
  4. return result;
  5. } else {
  6. HashMap result = new HashMap();
  7. try {
  8. //扫描所有 Jar 包类路径下的 META-INF/spring.factories 文件
  9. Enumeration urls = classLoader.getResources("META-INF/spring.factories");
  10. while(urls.hasMoreElements()) {
  11. URL url = (URL)urls.nextElement();
  12. UrlResource resource = new UrlResource(url);
  13. //将扫描到的 META-INF/spring.factories 文件中内容包装成 properties 对象
  14. Properties properties = PropertiesLoaderUtils.loadProperties(resource);
  15. Iterator var6 = properties.entrySet().iterator();
  16. while(var6.hasNext()) {
  17. Map.Entry<?, ?> entry = (Map.Entry)var6.next();
  18. //提取 properties 对象中的 key 值
  19. String factoryTypeName = ((String)entry.getKey()).trim();
  20. //提取 proper 对象中的 value 值(多个类的完全限定名使用逗号连接的字符串)
  21. // 使用逗号为分隔符转换为数组,数组内每个元素都是配置类的完全限定名
  22. String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
  23. String[] var10 = factoryImplementationNames;
  24. int var11 = factoryImplementationNames.length;
  25. //遍历配置类数组,并将数组转换为 list 集合
  26. for(int var12 = 0; var12 < var11; ++var12) {
  27. String factoryImplementationName = var10[var12];
  28. ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
  29. return new ArrayList();
  30. })).add(factoryImplementationName.trim());
  31. }
  32. }
  33. }
  34. //将 propertise 对象的 key 与由配置类组成的 List 集合一一对应存入名为 result 的 Map 中
  35. result.replaceAll((factoryType, implementations) -> {
  36. return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  37. });
  38. cache.put(classLoader, result);
  39. //返回 result
  40. return result;
  41. } catch (IOException var14) {
  42. throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
  43. }
  44. }
  45. }
posted @ 2022-05-06 23:23  小强哥in  阅读(15)  评论(0编辑  收藏  举报