環境:Spring5.3.23
在大型的Spring項目中,由于有成百上千的Bean需要通過掃描注冊到Spring容器中,這會導致啟動速度變慢。為了解決這個問題,我們可以使用spring-context-indexer來優化啟動速度。
spring-context-indexer是一個工具,它可以在編譯時為類路徑下的組件創建索引,這樣在啟動時就可以通過索引快速地加載和初始化組件。使用spring-context-indexer可以大大提升Spring應用程序的啟動速度,從而使得開發人員可以更快地開發和測試應用程序,提高開發效率。
在大型項目中,由于Bean數量眾多,Spring應用程序的啟動時間可能會變得非常長。通過使用spring-context-indexer,我們可以減少啟動時間,從而減少對系統資源的占用,使得更多的資源可以被用來處理其他任務。此外,快速啟動應用程序還可以減少因為程序長時間未響應而導致的故障和錯誤率。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <version>5.3.23</version> <optional>true</optional></dependency>
如果使用的是gradle
# gradle 4.5以下版本包括4.5dependencies { compileOnly "org.springframework:spring-context-indexer:5.3.23"}# gradle 4.6以上版本dependencies { annotationProcessor "org.springframework:spring-context-indexer:5.3.23"}
@Componentpublic class Person {}@Componentpublic class Student {}@Componentpublic class User {}
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.pack.context_indexed")) { for (String name : context.getBeanDefinitionNames()) { System.out.println(name) ; } }}
控制臺輸出
org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.annotation.internalPersistenceAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactorypersonstudentuser
所有的bean都能被容器掃描到
內容如下
com.pack.context_indexed.Person=org.springframework.stereotype.Component
格式:完整的包名=完整注解名
有了上面的索引文件后,再次運行上面的測試文件
# ...person
自定義的bean就只剩下person了,這就是因為在上面的索引文件中只定義了 person的原因,這樣就不會在掃描你當前包下的所有class文件了,只會讀取索引文件中的內容。
此時如果你訪問不在此列表中的類,程序將報錯,找不到對應的Bean對象。
我們可以在索引文件中使用自己定義的注解,示例如下
// 自定義注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Componentpublic @interface PackComponent {}// 修改User類注解@PackComponentpublic class User {}
com.pack.context_indexed.Person=org.springframework.stereotype.Componentcom.pack.context_indexed.User=com.pack.context_indexed.PackComponent
控制臺輸出
# ...personuser
以上都是通過手動創建的方式,在實際大型項目中如果你手動創建維護索引文件那還不如不使用索引,并且還及其容易出現錯誤。我們可以借助IDE工具配置注解處理器來幫我們自動的完成索引文件的創建。
這里以Eclipse為例來配置
首先,將spring-context-indexer添加eclipse注解處理中
圖片
通過上面的1,2,3步后,索引文件將會被自動的生成。
自動生成的spring.components文件,默認將在target/classes/META-INF目錄下,部分內容:
com.pack.context_indexed.Persnotallow=org.springframework.stereotype.Componentcom.pack.context_indexed.Student=org.springframework.stereotype.Componentcom.pack.context_indexed.User=org.springframework.stereotype.Component
關閉索引功能
我們可以通過設置JVM參數進行關閉索引功能,在啟動程序添加如下參數即可關閉
-Dspring.index.ignore=true
在大型Spring項目中,由于Bean數量眾多,導致啟動速度變慢。使用spring-context-indexer可以優化啟動速度,提高開發效率、減少資源占用和減少故障、錯誤率。spring-context-indexer是一個工具,它可以在編譯時為類路徑下的組件創建索引,這樣在啟動時就可以通過索引快速地加載和初始化組件。
本文鏈接:http://www.www897cc.com/showinfo-26-38514-0.html優化技巧:如何加快Spring項目啟動速度
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 理解 Wasm 基礎概念,了解 Wasm 是如何被加載運行的?
下一篇: 手把手教你寫設計方案,你學明白了嗎?