最小化Spring Boot启动时间 [英] Minimise Spring Boot Startup Time

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

问题描述

在我看来,SpringBoot项目需要很长时间才能加载。这可能是因为SpringBoot正在为您配置组件,其中一些甚至可能不需要。
最明显的做法是从类路径中删除不必要的依赖项。但是,这还不够。

In my opinion SpringBoot projects take a long time to load. This probably happens because SpringBoot is configuring components for you, some of which you might not even need. The most obvious thing to do is to remove unnecessary dependancies from your class path. However, that is not enough.

有没有办法找出SpringBoot正在配置哪些模块来挑选你不需要的东西并禁用它们?

Is there any way to find out which modules SpringBoot is configuring for you to pick out what you don't need and disable them?

一般来说,还有什么方法可以加快SpringBoot应用程序的启动时间吗?

Is there anything else one can do to speed up startup time for SpringBoot applications in general?

推荐答案

我可以告诉你,我通过Spring MVC,JMS,Atomikos事务,Hibernate,JMX支持和嵌入式Tomcat使用restful webservices运行大型(800,000多行代码)应用程序。尽管如此,应用程序将在大约19秒内在我的本地桌面上启动。

I can tell you that I run a large (800,000+ lines of code) application, using restful webservices via Spring MVC, JMS, Atomikos transaction, Hibernate, JMX support, and embedded Tomcat. With all that, the application will start on my local desktop in about 19 seconds.

Spring Boot努力不配置您不使用的模块。但是,很容易引入您不想要的其他依赖项和配置。

Spring Boot tries hard not to configure modules you are not using. However, it is easy to introduce additional dependencies and configuration that you did not intend.

请记住,Spring Boot遵循约定优于配置范例,只需在类路径中放置库就可以使Spring Boot尝试配置模块以使用库。另外,通过使用@RestController注释类的简单操作将触发Spring Boot自动配置整个Spring MVC堆栈。

Remember that Spring Boot follows the convention over configuration paradigm and by simply placing a library in your class path can cause Spring Boot to attempt to configure a module to use the library. Also, by doing something as simple as annotating your class with @RestController will trigger Spring Boot to auto-configure the entire Spring MVC stack.

你可以看到发生了什么在封面下启用调试日志记录,就像从命令行启动应用程序时指定 - debug 一样简单。您还可以在application.properties中指定debug = true。

You can see what is going on under the covers and enable debug logging as simple as specifying --debug when starting the application from the command-line. You can also specify debug=true in your application.properties.

此外,您可以在 application.properties 简单如下:

In addition, you can set the logging level in application.properties as simple as:

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

如果检测到你不想要的自动配置模块,它可以禁用。可在此处找到相关文档: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration

If you detect an auto-configured module you don't want, it can be disabled. The docs for this can be found here: 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 {
}

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

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