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


    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注解

    为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
    学问:纸上得来终觉浅,绝知此事要躬行
    为事:工欲善其事,必先利其器。
    态度:道阻且长,行则将至;行而不辍,未来可期
    转载请标注出处!
  • 相关阅读:
    Oracle分页问题
    win10系统vs2008环境wince项目无法创建问题
    工作满十年了
    让Vs2013 完美支持EF6.1 Code First with Oracle 2015年12月24日更新
    Oracle DMP 操作笔记之根据DMP逆向推导出导出的表空间名称
    【转】如何在 Eclipse 中進行 TFS 的版本管控
    【转】什麼是 Team Explorer Everywhere 2010 ?TFS 專用的 Eclipse 整合套件的安裝與設定
    [转]有关USES_CONVERSION
    [转]使用VC/MFC创建一个线程池
    IT男的”幸福”生活
  • 原文地址:https://www.cnblogs.com/ios9/p/14887844.html
Copyright © 2020-2023  润新知