暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Springboot使用@Indexed注解提升启动性能

为了offer 2021-12-02
3504

虽然类路径扫描非常快,但可以通过在编译时创建一个静态候选列表来提高大型应用程序的启动性能。在这种模式下,所有作为组件扫描目标的模块都必须使用这种机制。

maven依赖

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.1.12.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>


对于 Gradle 4.5 及更早版本,应在compileOnly
 配置中声明依赖项,如以下示例所示:

dependencies {
compileOnly "org.springframework:spring-context-indexer:5.1.12.RELEASE"
}

对于 Gradle 4.6 及更高版本,应在annotationProcessor
 配置中声明依赖项,如以下示例所示:

dependencies {
annotationProcessor "org.springframework:spring-context-indexer:5.1.12.RELEASE"
}

该过程会生成一个META-INF/spring.components
包含在 jar 文件中的文件。

当 ApplicationContext检测到这样的索引时,它会自动使用它而不是扫描类路径。


当Spring应用上下文执行ComponentScan
扫描时,META-INT/spring.components
将会被CandidateComponentsIndexLoader
 读取并加载,转换为CandidateComponentsIndex
对象,这样的话@ComponentScan
不在扫描指定的package,而是读取CandidateComponentsIndex
对象,从而达到提升性能的目的。


文章转载自为了offer,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论