加快 Spring Boot 启动时间 [英] Speed up Spring Boot startup time

查看:53
本文介绍了加快 Spring Boot 启动时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序.我添加了很多依赖项(不幸的是,看起来我需要所有依赖项)并且启动时间增加了很多.仅仅做一个 SpringApplication.run(source, args) 需要 10 秒.

I have a Spring Boot application. I've added a lot of dependencies (unfortunately, looks I need all of them) and the startup time went up quite a lot. Just doing a SpringApplication.run(source, args) takes 10 seconds.

虽然与习惯"相比,这可能并不多,但我很不高兴它需要那么多,主要是因为它破坏了开发流程.此时应用程序本身相当小,所以我假设大部分时间与添加的依赖项有关,而不是应用程序类本身.

While that might not be much compared to what are "used" to, I'm unhappy that it takes that much, mostly because it breaks the development flow. The application itself is rather small at this point, so I assume most of the time is related to the added dependencies, not the app classes themselves.

我认为问题是类路径扫描,但我不确定如何:

I assume the issue is classpath scanning, but I am not sure how to:

  • 确认这是问题所在(即如何调试"Spring Boot)
  • 如果真的是原因,我该如何限制它,让它变得更快?例如,如果我知道某些依赖项或包不包含 Spring 应该扫描的任何内容,有没有办法限制它?

我假设 增强 Spring 在启动期间进行并行 bean 初始化 会加快速度,但该增强请求自 2011 年以来一直处于开放状态,没有任何进展.我看到 Spring Boot 本身的一些其他努力,例如 Investigate Tomcat JarScanning 速度改进,但这是 Tomcat 特有的,已被放弃.

I assume that enhancing Spring to have parallel bean initialization during startup would speed up things, but that enhancement request has been open since 2011, without any progress. I see some other efforts in Spring Boot itself, such as Investigate Tomcat JarScanning speed improvements, but that is Tomcat specific and has been abandoned.

本文:

虽然针对集成测试,建议使用 lazy-init=true,但是我不知道如何使用 Java 配置将其应用于 Spring Boot 中的所有 bean - 这里有任何指针吗?

although aimed at integration tests, suggests using lazy-init=true, however I do not know how to apply this to all beans in Spring Boot using Java configuration - any pointers here?

欢迎任何(其他)建议.

Any (other) suggestions would be welcome.

推荐答案

Spring Boot 做了很多可能不需要的自动配置.因此,您可能只想缩小应用程序所需的自动配置范围.要查看包含的自动配置的完整列表,只需在调试模式下运行 org.springframework.boot.autoconfigure 的日志记录(logging.level.org.springframework.boot.autoconfigure=DEBUGapplication.properties 中的代码>).另一种选择是使用 --debug 选项运行 Spring Boot 应用程序:java -jar myproject-0.0.1-SNAPSHOT.jar --debug

Spring Boot does a lot of auto-configuration that may not be needed. So you may want to narrow down only auto-configuration that is needed for your app. To see full list of auto-configuration included, just run logging of org.springframework.boot.autoconfigure in DEBUG mode (logging.level.org.springframework.boot.autoconfigure=DEBUG in application.properties). Another option is to run spring boot application with --debug option: java -jar myproject-0.0.1-SNAPSHOT.jar --debug

输出中会有这样的东西:

There would be something like this in output:

=========================
AUTO-CONFIGURATION REPORT
=========================

检查此列表并仅包含您需要的自动配置:

Inspect this list and include only autoconfigurations you need:

@Configuration
@Import({
        DispatcherServletAutoConfiguration.class,
        EmbeddedServletContainerAutoConfiguration.class,
        ErrorMvcAutoConfiguration.class,
        HttpEncodingAutoConfiguration.class,
        HttpMessageConvertersAutoConfiguration.class,
        JacksonAutoConfiguration.class,
        ServerPropertiesAutoConfiguration.class,
        PropertyPlaceholderAutoConfiguration.class,
        ThymeleafAutoConfiguration.class,
        WebMvcAutoConfiguration.class,
        WebSocketAutoConfiguration.class,
})
public class SampleWebUiApplication {

代码复制自这篇博文.

这篇关于加快 Spring Boot 启动时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆