Explain Codes LogoExplain Codes Logo

Speed up Spring Boot startup time

java
spring-boot
performance-optimization
class-loading
Alex KataevbyAlex Kataev·Mar 6, 2025
TLDR

To reduce Spring Boot startup time, turn on lazy initialization with spring.main.lazy-initialization=true. Don't let your app get bloated - throw out unnecessary auto-configurations via spring.autoconfigure.exclude. You can also tweak your JVM using the flags -Xverify:none or -noverify to bypass class verification, making your program load lightening fast! 🚀

spring: main: lazy-initialization: true // better late than never, right? autoconfigure: exclude: // Marie Kondo your code! - org.springframework.boot.autoconfigure... // Check your project for uninvited guests!

Using lazy initialization, selective auto-configuration, and the right JVM options, you'll see your startup times go on a diet and become remarkably lean.

Shave off milliseconds

Make your classpath a VIP List

Limit your classpath to essential resources - don't let gatecrashers slow down your app. The spring-boot-thin-launcher can help you create a more efficient fat jar, reducing your application size and improving class load times.

Keep track of your progress

Don't fly blind - use the JMH (Java Microbenchmark Harness) to measure your application's startup time accurately. With this compass, you can spot slow sections and give them a speed boost.

Consolidate your properties

Set spring.config.location properly to make property file management slick and efficient. You can also set up profile-specific properties to avoid unnecessary status checks during startup, it's like a fast pass to the front of the line.

Check your beans

With the LazyInitBeanFactoryPostProcessor you can measure bean initialization times. If you're working with older Spring versions, this is like a fitness tracker for your beans.

Beautify your logs

Specify log levels for specific packages to focus your debugging efforts. Lessen logging overhead and your startup time will feel lighter than a feather.

Further boosts and improvements

Smoothen the hostname highway

Address slow hostname resolution - it's like clearing traffic from your startup highway. With proper DNS settings, your startup will feel like it's on an open autobahn.

New allies: Spring Boot 2.4.0

Leverage the Spring Boot Startup endpoint from version 2.4.0. It will allow you to peek into your startup phases and tweak it for optimal speed.

JSON: Choose your player

Consider Gson over Jackson for a possible speed boost. It's like a game - pick the player that gives your team the edge (in this case, your application).

Quick classpath scan, pronto!

Use the spring-context-indexer for a whiz-bang classpath scan. Say hello to efficient bean loading, and bid adieu to sluggish starts.

JMX: Use it, don't abuse it

If JMX isn't necessary, just put spring.jmx.enabled=false. Are you JMX-ing all the way to slow-land? Not anymore!