SpringFactoriesLoader Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 全路径 : org.springframework.core.io.support.SpringFactoriesLoader 测试类 : org.springframework.core.io.support.SpringFactoriesLoaderTests loadFactories 加载并实例化工厂 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader) { Assert.notNull(factoryType, "'factoryType' must not be null"); ClassLoader classLoaderToUse = classLoader; if (classLoaderToUse == null) { classLoaderToUse = SpringFactoriesLoader.class.getClassLoader(); } // 工厂实现类名称 List<String> factoryImplementationNames = loadFactoryNames(factoryType, classLoaderToUse); if (logger.isTraceEnabled()) { logger.trace("Loaded [" + factoryType.getName() + "] names: " + factoryImplementationNames); } List<T> result = new ArrayList<>(factoryImplementationNames.size()); for (String factoryImplementationName : factoryImplementationNames) { // 将实例化的工厂放入结果集合 result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse)); } // 排序 AnnotationAwareOrderComparator.sort(result); return……
阅读全文