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

查看:359
本文介绍了加快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本身中看到了其他一些工作,例如调查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的日志记录(在application.properties中为logging.level.org.springframework.boot.autoconfigure=DEBUG).另一个选择是使用--debug选项运行应用程序: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天全站免登陆