Spring boot 启动性能优化
spring boot启动性能优化
spring boot中使用spring-context-indexer加快启动速度
Spring中@ComponentScan扫描的package包含的类越多的时候,Spring模式注解解析耗时就越长,服务启动时候就越长,针对此问题Spring提供了@Indexed注解来添加索引。
查看@Serive、@Controller、@Repository、@Component注解源码会发现已经自动添加了此@Indexed注解
1 @Target(ElementType.TYPE) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @Indexed 5 public @interface Component { 6 7 /** 8 * The value may indicate a suggestion for a logical component name, 9 * to be turned into a Spring bean in case of an autodetected component. 10 * @return the suggested component name, if any (or empty String otherwise) 11 */ 12 String value() default ""; 13 14 }
所以只需要在项目的添加spring-context-indexer的依赖即可
maven中:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <optional>true</optional> </dependency> </dependencies>
这样启动效率就提升了。
会发现在编译后生成了META-INF/spring.components文件
@Indexed,可以预编译,跟lombok一样在编译期处理。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?