优化SpringBoot程序启动速度的实现

2023-01-16 10:10:01
目录
一、延迟初始化Bean二、创建扫描索引三、升级SpringBoot新版本

Spring Boot 程序优化

一、延迟初始化Bean

一般在>spring.main.lazy-initialization属性,配置为 true 会将所有 Bean 延迟初始化。

spring:
  main:
    lazy-initialization: true

二、创建扫描索引

Spring5>spring-context-indexer功能,通过提前生成@ComponentScan的扫描索引,解决在类过多时导致扫描速度过慢的问题。
我们只需要将依赖引入,然后在启动类上使用@Indexed注解即可。这样在程序编译打包之后会生成META-INT/spring.components文件,当执行@ComponentScan扫描类时,会读取索引文件,提高扫描速度。

<dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-context-indexer</artifactId>
  	<optional>true</optional>
</dependency>
@Indexed
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

在这里插入图片描述

三、升级SpringBoot新版本

SpringBoot每次升级都会对性能进行一些优化,目前最新版已经来到了3,Spring官方对性能优化做的已经非常好,能大大提高程序的编译以及启动速度。

到此这篇关于优化SpringBoot程序启动速度的实现的文章就介绍到这了,更多相关优化SpringBoot程序启动速度内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!