亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring FactoriesLoader機制實例詳解

瀏覽:56日期:2023-09-16 10:06:46

一、SpringFactoriesLoader 介紹

1.1 SpringFactoriesLoader 簡介

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

Properties 類型格式:

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

value:是實現,多個實現通過 **逗號** 進行分隔

1.2 SpringFactoriesLoader 常用方法

loadFactoryNames

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

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

1.3 loadFactories 流程圖

Spring FactoriesLoader機制實例詳解

二、SpringFactoriesLoader 源碼解析

2.1 loadFactoryNames 解析

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { // 獲取包含包名的工廠類名稱 String factoryTypeName = factoryType.getName(); // 獲取所有配置在 META-INF/spring.factories 文件的值 // 然后獲取指定類的實現類名列表 return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());}

// 默認的工廠配置路徑地址,可以存放在多個 JAR 包下public static final String FACTORIES_RESOURCE_LOCATION = 'META-INF/spring.factories';private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { // 判斷是否有緩存結果,如果有直接返回 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 文件,將其內容加載到一個 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();// 將逗號分割的屬性值逐個取出,然后放到 結果result 中去for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryTypeName, factoryImplementationName.trim());} } } // 將結果存放到緩存中 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'); // 如果未指定類加載器,則使用默認的 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); } // 創建結果集 List<T> result = new ArrayList<>(factoryImplementationNames.size()); for (String factoryImplementationName : factoryImplementationNames) { // 實例化工廠類,并添加到結果集中 result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse)); } // 對結果集列表進行排序 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); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 小明明看看视频永久免费网 | 国产成人精品三区 | 国产欧美综合在线 | 老妇毛片 | 色一情一伦一区二区三 | 亚洲特黄| 国产精品国产三级国产专区5o | 免费在线观看a级毛片 | 男女免费爽爽爽在线视频 | 日韩高清专区 | a毛片免费全部播放完整成 a毛片免费观看 | 国产精品成人h片在线 | 国产丝袜诱惑 | 天天色影院 | 免费观看日批视频 | 黑人猛男大战俄罗斯白妞 | 欧美成年免费a级 | 亚洲一区二区三区在线观看蜜桃 | 亚洲一欧洲中文字幕在线 | 国产羞羞事1000部在线观看 | 一级毛片大全 | 国产大片线上免费观看 | 国产精品美女免费视频观看 | 成人网在线看 | 成人免费黄色大片 | 国产a级三级三级三级中国 国产a级午夜毛片 | 亚洲国产韩国一区二区 | 国产系列在线观看 | 一级黄色录像免费 | 亚洲精品美女久久久久99 | 亚洲国产日韩女人aaaaaa毛片在线 | 国产大学生一级毛片绿象 | 欧美xxxx69tube8护士 | 成人国产精品免费网站 | 亚洲婷婷网 | 亚洲精品国产经典一区二区 | 亚洲精品美女 | 国产精品酒店视频免费看 | 国产精品久久国产精品99 gif | 欧美日本二区 | 91视频视频 |