Spring boot taking long time to start
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Spring Boot is a popular framework for building stand-alone, production-grade Spring-based applications quickly and easily. However, as powerful as it is, developers sometimes experience longer startup times, especially in larger applications. This scenario can be frustrating and may impact the overall development and deployment cycle. Here, we will delve into why Spring Boot might take a long time to start and provide some potential solutions to mitigate this issue.
Reasons for Slow Startup
- Auto-configuration: Spring Boot's opinionated setup comes with an auto-configuration feature intended to simplify application configuration based on your project's dependencies. However, this might be one of the reasons for a slow startup as Spring has to evaluate numerous conditions to configure your application automatically.
- Classpath scanning: During startup, Spring Boot scans the classpath to find components, configurations, and other necessary beans. If the application has a large classpath or numerous components, this process can significantly increase the startup time.
- Hibernate and JPA: Applications using Hibernate or JPA spend significant time during startup on entity scanning and creating the entity manager factory. Hibernate also uses a technique called "schema validation" that validates each table and column mapped in the entities against the existing schema in the database.
- Serialization: If your application has several beans or configurations that need to be serialized or checked for serialization compatibility, this can lengthen the startup time.
- Resource files loading: Loading various resources like XML, properties files, or YAML configurations can also affect the startup time. Large files or numerous small files can take considerable time to load and parse.
- DevTools: If you're using Spring Boot DevTools for local development, it may cause increased startup time because it adds a layer to restart the context whenever changes are detected.
Profiling Spring Boot Startup
To effectively reduce the startup time, the first step is profiling to understand what is taking the time. Spring Boot provides Actuator endpoints that give insights into what beans are loaded, which conditions were evaluated and how long each of these took during the application startup. Another tool is the Spring Boot Admin, which can help in visualizing detailed metrics.
Strategies to Speed Up Startup
Here are some strategies to help improve the startup time of a Spring Boot application:
- Lazy initialization: By default, Spring Framework initializes all singleton beans on startup to ensure that errors in the configuration are caught early. However, enabling lazy initialization can reduce startup times as beans will only be instantiated when they are first requested.
- Reduce auto-configurations: Customizing Spring Boot's auto-configuration or explicitly specifying configurations can prevent unnecessary configurations from being loaded.
- Limit classpath scanning: Use filters with
@ComponentScanto limit the scanning to specific packages, thus reducing the startup time. - Optimize Hibernate/JPA setup: Disabling schema validation in development or using lightweight alternatives for specific profiles can speed up the startup.
- Profile-specific configuration: Different configurations for different environments; for instance, reducing logging levels and disabling certain management endpoints in the development environment can enhance startup speed.
Impact Analysis Table
| Aspect | Impact on Startup Time | Potential Solution |
| Auto-configuration | High | Customize or disable specific auto configurations |
| Classpath scanning | High | Limit scanning using filters |
| Hibernate/JPA setup | Moderate to High | Disable schema validation in dev; optimize configurations |
| DevTools | Moderate | Disable or use selectively |
| Resource files loading | Moderate | Optimize resource handling |
Conclusion
While Spring Boot offers a lot of out-of-the-box functionalities and automation through auto-configurations, developers must still carefully consider and optimize these features to manage startup times effectively. Through profiling, judicious configuration, and selective use of features, the performance can often be significantly enhanced, leading to a better development workflow and faster deployment cycles.
Related reading
- Spring CrudRepository findByInventoryIds(List<Long> inventoryIdList) - equivalent to IN clause
- Spring Hibernate Query Plan Cache Memory usage
- Spring Webflux vs Vert.x
- SQL multiple column ordering
- Spring boot test configuration
- Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
- Spring boot Unable to start embedded Tomcat servlet container
- Spring Boot Unit Test ignores logging.level

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.