/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Spring5--@Indexed注解加快启动速度

* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com

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 对象,从而达到提升性能的目的。

在Spring5.0当中的@Component注解当中,已经添加了该注解:

复制代码
复制代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     */
    String value() default "";

}
复制代码
复制代码

 

参考:Spring5--@Indexed注解

posted @   一品堂.技术学习笔记  阅读(211)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示

目录导航