Spring中@ComponentScan扫描的package包含的类越多的时候,Spring模式注解解析耗时就越长,服务启动时候就越长,针对此问题Spring提供了@Indexed注解来添加索引。
查看@Serive、@Controller、@Repository、@Component注解源码会发现已经自动添加了此注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Indexed
public @interface Component {
String value() default "";
}
在项目的添加spring-context-indexer的依赖
maven中:
org.springframework spring-context-indexer true
gradle中
annotationProcessor 'org.springframework:spring-context-indexer'
gradle4.5以下使用
compileonly "org.springframework:spring-context-indexer"
这样启动效率就提升啦。
会发现在编译后生成了meta-INF/spring.components文件
@Indexed,可以预编译,跟lombok一样在编译期处理。



