Spring如何使用@Indexed加快启动速度
作者:Dongguabai 时间:2022-05-02 10:50:40
使用@Indexed加快启动速度
Spring读取@Component组件(派生性),有两种实现方式,一种是反射,一种是ASM。反射性能低主要是要loadClass,毕竟Class,需要Load,比如扫描”com.dongguabai”需要把所有的类load,效率太慢。
ASM相当于直接加载类的资源信息,ReadResource。
Spring5.0 后面又有了@Indexed,可以预编译。
会生成spring.compoments文件:
跟lombok一样在编译期处理。
Spring5--@Indexed注解
Spring Framework 5.0作为 Spring Boot 2.0 的底层核心框架,就目前已经发布的版本来看,相对于 Spring Framework 4.x 而言,注解驱动的性能提升不是那么明显。
然而随着 Spring Framework 注解驱动能能力逐渐受到开发人员的关注,尤其在 Spring Boot 应用场景中,大量使用注解 @CompoentScan 扫描指定的 package,当扫描的 package 所包含的类越多时,Spring 模式注解解析的耗时就越长。
对于这个问题,Spring Framework 5.0 版本引入的注解 @Indexed,为 Spring 模式注解添加索引,以提升应用启动性能。
举个栗子
@Indexed
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {}
但是,注解 @Indexed 不能孤立地存在,需要在工程 pom.xml 中增加 org.springframework:spring-context-indexer 依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
当工程打包为 JAR 或在 IDE 工具中重新构建后,METE-INF/spring.components 文件将自动生成。
换言之,该文件在编译时生成。当 Spring 应用上下文执行 @CompoentScan 扫描时,METE-INF/spring.components 将被 CandidateComponentsIndexLoader 读取并加载,转化为 CandidateComponentsIndex 对象,进而 @CompoentScan 不再扫描指定的 package,而是读取 CandidateComponentsIndex 对象,从而达到提升性能的目的。
来源:https://dongguabai.blog.csdn.net/article/details/83961406
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
AndroidStudio接入Unity工程并实现相互跳转的示例代码
![](https://img.aspxhome.com/file/2023/6/84736_0s.gif)
Java探索之Thread+IO文件的加密解密代码实例
JAVA通过Filter实现允许服务跨域请求的方法
Android如何让WebView中的HTML5页面实现视频全屏播放
![](https://img.aspxhome.com/file/2023/1/82401_0s.jpg)
避免sql注入_动力节点Java学院整理
Java 单例模式的实现资料整理
@ConfigurationProperties加载外部配置方式
![](https://img.aspxhome.com/file/2023/5/59385_0s.png)
Android中bindService基本使用方法概述
![](https://img.aspxhome.com/file/2023/6/85446_0s.png)
用Flutter开发自定义Plugin的方法示例
![](https://img.aspxhome.com/file/2023/3/99793_0s.png)
JWT在OpenFeign调用中进行令牌中继详解
Spring Boot Thymeleaf实现国际化的方法详解
![](https://img.aspxhome.com/file/2023/8/59008_0s.png)
Android RecyclerView基本使用详解
![](https://img.aspxhome.com/file/2023/6/86016_0s.gif)
Spring常用注解汇总
详解通过JDBC进行简单的增删改查(以MySQL为例)
![](https://img.aspxhome.com/file/2023/5/58035_0s.png)
SpringBoot中@Import注解如何正确使用
![](https://img.aspxhome.com/file/2023/7/57707_0s.png)
Spring Boot使用Allatori代码混淆的方法
![](https://img.aspxhome.com/file/2023/0/59080_0s.jpg)
Android Studio 多层级 Module 对 aar 引用问题解决方法
![](https://img.aspxhome.com/file/2023/2/86762_0s.png)
Java SE求解汉诺塔问题的示例代码
![](https://img.aspxhome.com/file/2023/1/61291_0s.png)