SpringBoot?spring.factories加載時機分析_今日熱搜
時間:2023-06-24 16:49:46
目錄
spring.factories作用源碼解析用法spring.factories作用
這個類似于Java中的SPI功能,SpringBoot啟動的時候會讀取所有jar包下面的META-INF/spring.factories文件;
并且將文件中的 接口/抽象類 對應的實現類都對應起來,并在需要的時候可以實例化對應的實現類
下面我們來分析一下源碼看看spring.factories的使用場景
(相關資料圖)
源碼解析
啟動SpringApplication,看看構造方法
public SpringApplication(ResourceLoader resourceLoader, Class>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
其中方法getSpringFactoriesInstances( ApplicationContextInitializer.class)是用于獲取Spring中指定類實例用的;并且獲取的時候是根據讀取整個項目中文件路徑為META-INF/spring.factories中的內容實例化對應的實例類的;
例如這里的ApplicationContextInitializer是一個接口,那么應該實例化哪些他的實現類呢?那就找META-INF/spring.factories文件 ; 那么我們在spring-boot:2.1.0jar包中找到了這個文件
讀取到需要實例化的實現類為
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer,\ org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\ org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
并且還在spring-boot-autoconfigure-2.1.0.RELEASE.jar中找到了這個文件
那么文件中的兩個實現類也會被實例化;加上上面4個總共有6個
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\ org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
可以看到不僅僅只是把org.springframework.context.ApplicationContextInitializer 的實例類解析了出來;而是所有的都解析了出來并且保存下來了.下次其他的類需要被實例化的時候就可以直接從內存里面拿了;
上面過程拿到了實例類之后,接下來就是實例化的過程了
privateCollection getSpringFactoriesInstances(Class type, Class>[] parameterTypes, Object... args) { ClassLoader classLoader = getClassLoader(); // Use names and ensure unique to protect against duplicates Set names = new LinkedHashSet<>( SpringFactoriesLoader.loadFactoryNames(type, classLoader)); List instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names); AnnotationAwareOrderComparator.sort(instances); return instances; }
方法createSpringFactoriesInstances就是創建實例的過程;可以看到傳入了對應的接口類org.springframework.context.ApplicationContextInitializer ;接下來就會實例化 上面找到了對應的實現類;
privateList createSpringFactoriesInstances(Class type, Class>[] parameterTypes, ClassLoader classLoader, Object[] args, Set names) { List instances = new ArrayList<>(names.size()); for (String name : names) { try { Class> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); Constructor> constructor = instanceClass .getDeclaredConstructor(parameterTypes); T instance = (T) BeanUtils.instantiateClass(constructor, args); instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException( "Cannot instantiate " + type + " : " + name, ex); } } return instances; }
實例化的過程如果,沒有什么特別需要講解的;
上面有個方法AnnotationAwareOrderComparator.sort(instances);是用來排序所有實例的; 實現類需要實現 接口Ordered; getOrder返回的值越小,優先級更高
用法
知道spring.factories的用法之后, 那么我們就可以利用這個特性實現自己的目的;
例如我們也可以寫一個接口類ApplicationContextInitializer的實現類;
等等之類的;
以上就是SpringBoot spring.factories加載時機分析的詳細內容,更多關于spring.factories加載時機的資料請關注腳本之家其它相關文章!
相關稿件
SpringBoot?spring.factories加載時機分析_今日熱搜
測運勢2022年運勢免費,紫微免費算2022年運勢|環球觀察
觀察:每日熱聞!今日熱門!北京市教委:高溫天氣下合理調整學生室外活動時間 根據實際情況采取減課或停課措施 熱點評_全球播資訊 全球簡訊 每日觀點
新資訊:關系戶來了?勇士新秀的經紀人竟然是勇士總經理的親弟弟?
享譽:哈爾濱專業治療疤痕醫院排名[大膽公開]哈爾濱哪家醫院無痕祛疤好|當前消息
強化審判能力建設 延伸司法保障職能 ——新疆知識產權保護水平穩步提升 天天精選
【短訊】“瓦格納”首領稱其部隊已進入俄境內!一文梳理這場“叛亂”經過……
中國經濟中長期前景POLL(2023年5月/PB):經濟、政策雙平穩成新主流預期
威觀寧夏:保險賠付1400萬很多嗎?對于普通人來說,購買商業保險是最簡單有效的抵御風險的行為吧?
【世界播資訊】年銷粽子600萬個!古老“粽子村”里的變與不變
范丞丞的“自私”,讓觀眾看清白鹿的為人,《跑男》因此話題出圈 每日視點
世界熱資訊!移動硬盤格式化后能恢復數據嗎(請將磁盤插入可移動磁盤)
古裝廠牌到全能玩家,年均爆款的西嘻影業提供了影視公司的生存法
買手機就選16+512G大內存,這3款都是驍龍8+芯片,關鍵價格還不貴
突發!國泰航空一客機突發故障,11人逃生途中受傷送醫!女乘客還原驚恐一幕:有人打電話給父母一直哭,有一個媽媽抱著孩子一直說對不起……


