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