Java 最小化 Spring Boot 启动时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35709234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Minimise Spring Boot Startup Time
提问by Samantha Catania
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 项目需要很长时间才能加载。这可能是因为 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?
一般来说,还有什么办法可以加快 SpringBoot 应用程序的启动时间?
采纳答案by pczeus
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.
我可以告诉你,我运行了一个大型(800,000 多行代码)应用程序,通过 Spring MVC、JMS、Atomikos 事务、Hibernate、JMX 支持和嵌入式 Tomcat 使用 Restful Web 服务。有了这一切,应用程序将在大约 19 秒内在我的本地桌面上启动。
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 努力不配置您不使用的模块。但是,很容易引入您不想要的其他依赖项和配置。
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.
请记住,Spring Boot 遵循配置范式的约定,只需在类路径中放置一个库,就会导致 Spring Boot 尝试配置模块以使用该库。此外,通过使用 @RestController 对类进行注释这样简单的操作,将触发 Spring Boot 自动配置整个 Spring MVC 堆栈。
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.
您可以看到幕后发生的事情,并启用调试日志记录,就像--debug
在从命令行启动应用程序时指定一样简单。您还可以在 application.properties 中指定 debug=true。
In addition, you can set the logging level in application.properties
as simple as:
此外,您可以application.properties
简单地设置日志记录级别:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
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
如果您检测到不需要的自动配置模块,则可以将其禁用。可以在此处找到此文档:http: //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration
An example would look like:
一个例子看起来像:
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
回答by Mikhail Kholodkov
A few additional tips which may be helpful.
一些额外的提示可能会有所帮助。
- Use OpenJ9instead of Hotspot for development
- If you use Hibernate, set
hibernate.ddl-auto=none
instead ofupdate
- Set vmargs to
-Xquickstart
- If you use OpenJ9 - set vmargs to
-XX:TieredStopAtLevel=1 -noverify
- If you use Hotspot - use IDE build instead of Gradlebuild
- Use Undertow instead of Tomcat
- Don't abuse annotation processing tools (mapstruct, immutables...) which will slow down the build process
- 使用OpenJ9代替 Hotspot 进行开发
- 如果您使用Hibernate,请设置
hibernate.ddl-auto=none
而不是update
- 将 vmargs 设置为
-Xquickstart
- 如果您使用 OpenJ9 - 将 vmargs 设置为
-XX:TieredStopAtLevel=1 -noverify
- 如果您使用 Hotspot - 使用 IDE 构建而不是Gradle构建
- 使用Undertow 代替 Tomcat
- 不要滥用注释处理工具(mapstruct、immutables...),这会减慢构建过程
In addition:
此外:
As this articlerecommends use @ComponentScan(lazyInit = true)
for local dev environment.
由于本文推荐@ComponentScan(lazyInit = true)
用于本地开发环境。
TL;DR
TL; 博士
What we want to achieve is to enable the bean lazy loading only in your local development environment and leave eager initialization for production. They say you can't have your cake and eat it too, but with Spring you actually can. All thanks to profiles.
@SpringBootApplication public class LazyApplication { public static void main(String[] args) { SpringApplication.run(LazyApplication.class, args); } @Configuration @Profile("local") @ComponentScan(lazyInit = true) static class LocalConfig { } }
我们想要实现的是仅在您的本地开发环境中启用 bean 延迟加载,并为生产保留预先初始化。他们说你不能吃蛋糕也吃它,但在春天你真的可以。这一切都归功于个人资料。
@SpringBootApplication public class LazyApplication { public static void main(String[] args) { SpringApplication.run(LazyApplication.class, args); } @Configuration @Profile("local") @ComponentScan(lazyInit = true) static class LocalConfig { } }