環(huán)境:Spring5.3.23
在大型的Spring項(xiàng)目中,由于有成百上千的Bean需要通過掃描注冊到Spring容器中,這會(huì)導(dǎo)致啟動(dòng)速度變慢。為了解決這個(gè)問題,我們可以使用spring-context-indexer來優(yōu)化啟動(dòng)速度。
spring-context-indexer是一個(gè)工具,它可以在編譯時(shí)為類路徑下的組件創(chuàng)建索引,這樣在啟動(dòng)時(shí)就可以通過索引快速地加載和初始化組件。使用spring-context-indexer可以大大提升Spring應(yīng)用程序的啟動(dòng)速度,從而使得開發(fā)人員可以更快地開發(fā)和測試應(yīng)用程序,提高開發(fā)效率。
在大型項(xiàng)目中,由于Bean數(shù)量眾多,Spring應(yīng)用程序的啟動(dòng)時(shí)間可能會(huì)變得非常長。通過使用spring-context-indexer,我們可以減少啟動(dòng)時(shí)間,從而減少對(duì)系統(tǒng)資源的占用,使得更多的資源可以被用來處理其他任務(wù)。此外,快速啟動(dòng)應(yīng)用程序還可以減少因?yàn)槌绦蜷L時(shí)間未響應(yīng)而導(dǎo)致的故障和錯(cuò)誤率。
<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) ; } }}
控制臺(tái)輸出
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都能被容器掃描到
內(nèi)容如下
com.pack.context_indexed.Person=org.springframework.stereotype.Component
格式:完整的包名=完整注解名
有了上面的索引文件后,再次運(yùn)行上面的測試文件
# ...person
自定義的bean就只剩下person了,這就是因?yàn)樵谏厦娴乃饕募兄欢x了 person的原因,這樣就不會(huì)在掃描你當(dāng)前包下的所有class文件了,只會(huì)讀取索引文件中的內(nèi)容。
此時(shí)如果你訪問不在此列表中的類,程序?qū)?bào)錯(cuò),找不到對(duì)應(yīng)的Bean對(duì)象。
我們可以在索引文件中使用自己定義的注解,示例如下
// 自定義注解@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
控制臺(tái)輸出
# ...personuser
以上都是通過手動(dòng)創(chuàng)建的方式,在實(shí)際大型項(xiàng)目中如果你手動(dòng)創(chuàng)建維護(hù)索引文件那還不如不使用索引,并且還及其容易出現(xiàn)錯(cuò)誤。我們可以借助IDE工具配置注解處理器來幫我們自動(dòng)的完成索引文件的創(chuàng)建。
這里以Eclipse為例來配置
首先,將spring-context-indexer添加eclipse注解處理中
圖片
通過上面的1,2,3步后,索引文件將會(huì)被自動(dòng)的生成。
自動(dòng)生成的spring.components文件,默認(rèn)將在target/classes/META-INF目錄下,部分內(nèi)容:
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
關(guān)閉索引功能
我們可以通過設(shè)置JVM參數(shù)進(jìn)行關(guān)閉索引功能,在啟動(dòng)程序添加如下參數(shù)即可關(guān)閉
-Dspring.index.ignore=true
在大型Spring項(xiàng)目中,由于Bean數(shù)量眾多,導(dǎo)致啟動(dòng)速度變慢。使用spring-context-indexer可以優(yōu)化啟動(dòng)速度,提高開發(fā)效率、減少資源占用和減少故障、錯(cuò)誤率。spring-context-indexer是一個(gè)工具,它可以在編譯時(shí)為類路徑下的組件創(chuàng)建索引,這樣在啟動(dòng)時(shí)就可以通過索引快速地加載和初始化組件。
本文鏈接:http://www.www897cc.com/showinfo-26-38514-0.html優(yōu)化技巧:如何加快Spring項(xiàng)目啟動(dòng)速度
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com