Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimise Spring Boot Startup Time [duplicate]

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.

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?

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

like image 276
Samantha Catania Avatar asked Feb 29 '16 20:02

Samantha Catania


People also ask

How do I reduce startup time on spring boot?

spring.main.lazy-initialization=true Setting this property truely will initialize the bean only after it comes into the picture. This property will reduce the run time of the application. We can see that when the property is not applied, the running time is approximately around 8.4 seconds.

Why does spring boot take so long to start?

When a Spring Boot Application has slow startup, it can be one or more beans and related dependencies taking longer to initialise and slowing down the entire process. Profiling Spring Boot application doesn't often help in diagnosing the startup issues.

Does spring boot reduce development time?

Advantages of Spring Boot:It reduces lots of development time and increases productivity. It avoids writing lots of boilerplate Code, Annotations and XML Configuration. It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.


1 Answers

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 { } 
like image 75
pczeus Avatar answered Oct 06 '22 08:10

pczeus