優(yōu)化技巧:如何加快Spring項(xiàng)目啟動(dòng)速度
環(huán)境:Spring5.3.23
1. 介紹
在大型的Spring項(xiàng)目中,由于有成百上千的Bean需要通過(guò)掃描注冊(cè)到Spring容器中,這會(huì)導(dǎo)致啟動(dòng)速度變慢。為了解決這個(gè)問(wèn)題,我們可以使用spring-context-indexer來(lái)優(yōu)化啟動(dòng)速度。
spring-context-indexer是一個(gè)工具,它可以在編譯時(shí)為類路徑下的組件創(chuàng)建索引,這樣在啟動(dòng)時(shí)就可以通過(guò)索引快速地加載和初始化組件。使用spring-context-indexer可以大大提升Spring應(yīng)用程序的啟動(dòng)速度,從而使得開(kāi)發(fā)人員可以更快地開(kāi)發(fā)和測(cè)試應(yīng)用程序,提高開(kāi)發(fā)效率。
在大型項(xiàng)目中,由于Bean數(shù)量眾多,Spring應(yīng)用程序的啟動(dòng)時(shí)間可能會(huì)變得非常長(zhǎng)。通過(guò)使用spring-context-indexer,我們可以減少啟動(dòng)時(shí)間,從而減少對(duì)系統(tǒng)資源的占用,使得更多的資源可以被用來(lái)處理其他任務(wù)。此外,快速啟動(dòng)應(yīng)用程序還可以減少因?yàn)槌绦蜷L(zhǎng)時(shí)間未響應(yīng)而導(dǎo)致的故障和錯(cuò)誤率。
2. 配置使用
引入依賴包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.3.23</version>
<optional>true</optional>
</dependency>
如果使用的是gradle
# gradle 4.5以下版本包括4.5
dependencies {
compileOnly "org.springframework:spring-context-indexer:5.3.23"
}
# gradle 4.6以上版本
dependencies {
annotationProcessor "org.springframework:spring-context-indexer:5.3.23"
}
準(zhǔn)備Bean對(duì)象
@Component
public class Person {
}
@Component
public class Student {
}
@Component
public class User {
}
測(cè)試上面的的類都能被容器掃描到
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.pack.context_indexed")) {
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name) ;
}
}
}
控制臺(tái)輸出
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalPersistenceAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
person
student
user
所有的bean都能被容器掃描到
手動(dòng)創(chuàng)建META-INF/spring.components 文件
內(nèi)容如下
com.pack.context_indexed.Person=org.springframework.stereotype.Component
格式:完整的包名=完整注解名
有了上面的索引文件后,再次運(yùn)行上面的測(cè)試文件
# ...
person
自定義的bean就只剩下person了,這就是因?yàn)樵谏厦娴乃饕募兄欢x了 person的原因,這樣就不會(huì)在掃描你當(dāng)前包下的所有class文件了,只會(huì)讀取索引文件中的內(nèi)容。
此時(shí)如果你訪問(wèn)不在此列表中的類,程序?qū)?bào)錯(cuò),找不到對(duì)應(yīng)的Bean對(duì)象。
自定義注解支持
我們可以在索引文件中使用自己定義的注解,示例如下
// 自定義注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface PackComponent {
}
// 修改User類注解
@PackComponent
public class User {
}
在索引文件中添加配置
com.pack.context_indexed.Person=org.springframework.stereotype.Component
com.pack.context_indexed.User=com.pack.context_indexed.PackComponent
控制臺(tái)輸出
# ...
person
user
以上都是通過(guò)手動(dòng)創(chuàng)建的方式,在實(shí)際大型項(xiàng)目中如果你手動(dòng)創(chuàng)建維護(hù)索引文件那還不如不使用索引,并且還及其容易出現(xiàn)錯(cuò)誤。我們可以借助IDE工具配置注解處理器來(lái)幫我們自動(dòng)的完成索引文件的創(chuàng)建。
這里以Eclipse為例來(lái)配置
首先,將spring-context-indexer添加eclipse注解處理中
圖片
通過(guò)上面的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.Component
com.pack.context_indexed.Student=org.springframework.stereotype.Component
com.pack.context_indexed.User=org.springframework.stereotype.Component
關(guān)閉索引功能
我們可以通過(guò)設(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)速度,提高開(kāi)發(fā)效率、減少資源占用和減少故障、錯(cuò)誤率。spring-context-indexer是一個(gè)工具,它可以在編譯時(shí)為類路徑下的組件創(chuàng)建索引,這樣在啟動(dòng)時(shí)就可以通過(guò)索引快速地加載和初始化組件。