优化SpringBoot程序启动速度
2023-03-20 14:56:58 时间
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每次升级都会对性能进行一些优化,目前最新版已经来到了3
,Spring官方对性能优化做的已经非常好,能大大提高程序的编译以及启动速度。
相关文章
- Linux 性能监测工具
- Linux 性能监测:介绍
- 对中级 Linux 用户有用的 20 个命令
- Linux 性能监测:Memory
- Linux 性能监测:IO
- 实用的系统工具之 lsof
- 双态运维联盟工作会议暨2017年度双态运维大会乌镇峰会筹备会在新华三杭州园区召开
- 优化GNU/Linux桌面安装的九个步骤
- Linux 的启动流程
- Linux流量监控工具 – iftop
- Linux下chkconfig命令详解
- ClassicMenu Indicator 0.08 发布
- Google开源基于Deep Learning的word2vec工具
- 设计师的 Linux 桌面发行版 elementary OS
- 移动网站性能优化:网页加载技术概览
- 4 款 Linux 下免费好用的时钟软件
- 社会化海量数据采集爬虫框架搭建
- Linux 命令行快捷键
- KDE社区推ALERT开源工具集提升开发效率
- 实战低成本服务器搭建千万级数据采集系统