优化SpringBoot程序启动速度

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Spring Boot 程序优化

一、延迟初始化Bean

一般在 SpringBoot 中都拥有很多的耗时任务比如数据库建立连接、初始线程池的创建等等我们可以延迟这些操作的初始化来达到优化启动速度的目的。Spring Boot 2.2 版本后引入 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每次升级都会对性能进行一些优化目前最新版已经来到了3Spring官方对性能优化做的已经非常好能大大提高程序的编译以及启动速度。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring