spring-boot - 如何提高 Spring Boot 启动时间

标签 spring-boot

是否可以改善Spring Boot的启动时间?

这是一个空的 CommandLineRunner 项目的示例。启动时间为 914 毫秒。

@SpringBootApplication
public class Main implements CommandLineRunner {

    private static long t1;

    public static void main(String[] args) {
        t1 = System.currentTimeMillis();
        SpringApplication.run(Main.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(System.currentTimeMillis() - t1);    
    }

}

最佳答案

您可以使用Spring Framework 5.0组件索引功能作为类路径扫描的替代方法来缩短引导启动时间。

添加此支持是为了简化类路径扫描器中候选组件识别步骤。

应用程序构建任务可以为当前项目定义自己的 META-INF/spring.components 文件。

在编译时,会对源模型进行内省(introspection),并标记 JPA 实体和 Spring 组件。

对于类少于 200 个的小型项目,从索引读取实体而不是扫描类路径没有显着差异。

但是,

It has significant impacts on large projects. Loading the component index is cheap. Therefore, the startup time with the index remains constant as the number of classes increases.

您可以在此处找到更多详细信息JIRA

更新其他选项

Spring Boot 2.2 中的延迟初始化功能 Spring Boot 2.2 可以轻松启用延迟初始化。需要将 spring.main.lazy-initialization 设置为 true 以启用延迟初始化。

注意 - 惰性内化有一些优点和缺点,请谨慎使用。更多详情请查看This

关于spring-boot - 如何提高 Spring Boot 启动时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747060/

相关文章:

java - 使用 Spring Data JPA 运行存储过程时出现问题 - SQLServerException : The column name id is not valid

java - Spring 启动: Json Response is not Mapping to Entity

java - Spring Boot 2.3 Liveness Probe 功能在正常关闭时失败

spring-boot - Spring Webflux + JPA : Reactive Repositories are not supported by JPA

java - 用于安全 LDAP 身份验证绕过的 BlindSSLFactory 在生产环境中不起作用

mysql - 驱动 com.mysql.cj.jdbc.Driver 声称不接受 jdbcUrl

java - 部署 Spring .ear 时常量池标记无效

spring-boot - 如何在 Spring Boot 中使用 @Bean 创建或配置 Rest 模板

spring-boot - 1 spring boot 应用程序中的 Spring mvc 和 webflux

java - Spring boot - 没有嵌入式tomcat的Rest Call客户端