加快Spring启动时间

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

虽然与“使用”相比可能没有太大的差别,但我却不高兴,这主要是因为它打破了开发stream程。 应用程序本身在这一点上是相当小的,所以我认为大部分时间都与增加的依赖关系有关,而不是应用程序类本身。

我假设这个问题是类path扫描,但我不知道如何:

  • 确认是问题(即如何“debugging”Spring Boot)
  • 如果真的是这个原因,我怎么能限制它,所以它变得更快? 例如,如果我知道某些依赖项或包没有包含Spring应该扫描的任何内容,是否有限制的方法?

我假设:

  • https://jira.spring.io/browse/SPR-8767

会加快速度,但在这一点上还没有被分类。 我在Spring Boot本身看到了一些其他的努力,例如:

  • https://github.com/spring-projects/spring-boot/issues/1610

但是这看起来像Tomcat特有的。

本文:

  • http://www.nurkiewicz.com/2010/12/speeding-up-spring-integration-tests.html

虽然针对集成testing,build议使用lazy-init=true ,但是我不知道如何将这个应用到Spring Boot中所有使用Javaconfiguration的bean – 这里有什么指针?

任何(其他)build议将受到欢迎。

Spring Boot做了很多可能不需要的自动configuration。 所以你可能只想缩小你的应用程序所需的自动configuration。 要查看包含的自动configuration的完整列表,只需以DEBUG模式( application.properties logging.level.org.springframework.boot.autoconfigure=DEBUG )运行org.springframework.boot.autoconfigure日志logging。 另一种select是使用--debug选项运行spring启动应用程序: java -jar myproject-0.0.1-SNAPSHOT.jar --debug

输出中会有这样的东西:

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

检查这个列表,只包含你需要的自动configuration:

 @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引导启动时间

综上所述:

你可以看到底下发生了什么,启用debugging日志logging就像在命令行启动应用程序时指定–debug一样简单。 你也可以在你的application.properties中指定debug = true。

另外,您可以在application.properties中设置日志logging级别,如下所示:

logging.level.org.springframework.web:DEBUG logging.level.org.hibernate:ERROR

如果您检测到不需要的自动configuration的模块,则可以禁用该模块。 文档可以在这里find: http : //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration

一个例子看起来像:

 @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { } 

你可以试试我的库FastClasspathScanner 。 我不知道Spring是否能够使用它,但它比Spring更快地扫描类path。

只有一个确定的方法来处理这里发生的事情,那就是分析你的应用程序,寻找导致问题的热点。

我build议使用YourKit。 它具有非常好的IDE集成,所以你可以从Eclipse或其他任何内容来分析你的应用程序。 看这里

https://www.yourkit.com/docs/java/help/local_profiling.jsp

你很快就能看到是什么导致了问题,然后可以回到这里寻找可能的解决scheme。

对我来说,这听起来像你使用了一个错误的configuration设置。 首先检查我的容器和可能的冲突。 要确定谁正在使用最多的资源,您必须一次检查每个依赖关系的内存映射(请参阅数据量!),而且还需要大量的时间(以及SUDO权限)。 顺便说一句:你通常testing代码对依赖关系?