Minimise Spring Boot Startup Time [duplicate]

我是研究僧i 提交于 2019-11-28 17:54:09
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.

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.

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.

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.

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

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

An example would look like:

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

A few additional tips which may be helpful.

In addition:

As this article recommends use @ComponentScan(lazyInit = true) for local dev environment.

TL;DR

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 {
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!