10 Proven Ways to Speed Up Spring Boot Startup Time
This article presents ten practical techniques—including lazy loading, lazy database initialization, selective auto‑configuration, log level tuning, JVM flags, dependency trimming, bean optimization, JIT tweaks, classpath scanning reduction, and DevTools restart—to dramatically reduce Spring Boot application startup time.
Spring Boot is a widely used Java framework that simplifies Spring‑based application development, but its startup time can become a critical issue. The following practices help accelerate Spring Boot startup.
1. Lazy Loading
Spring Boot 2.2+ supports lazy loading, reducing CPU and memory usage during startup.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.setLazyInitialization(true); // Enable lazy loading
app.run(args);
}
}
// By setting setLazyInitialization to true, only essential beans are loaded at startup; others are created on first use.2. Lazy Database Initialization
Deferring database connection initialization shortens startup time.
# application.properties
spring.datasource.initialization-mode=lazy
// This setting makes the datasource initialize lazily.3. Reduce Auto‑Configuration Classes
Exclude unnecessary auto‑configuration to avoid unnecessary processing.
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication {
// ...
}
// The exclude attribute removes unneeded auto‑configuration classes.4. Optimize Log Levels
Excessive logging can degrade startup performance; set a reasonable log level.
# application.properties
logging.level.root=WARN
// Setting root log level to WARN reduces log output.5. JVM Parameter Tuning
JVM startup flags significantly affect Spring Boot launch time.
java -Xmx512m -Xms512m -jar myapp.jar
// Sets max and min heap size to 512 MB (adjust as needed).6. Trim Dependencies
Removing unused dependencies reduces the amount of code the JVM must load.
<!-- pom.xml -->
<!-- Example: remove an unnecessary Spring Boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>7. Optimize Spring Beans
Lazy‑loading beans can further improve startup speed.
@Component
@Lazy
public class HeavyService {
// ...
}
// The @Lazy annotation delays bean creation until first use.8. JIT Compilation Tuning
Adjusting the JIT compiler can reduce compilation overhead.
java -XX:TieredStopAtLevel=1 -jar myapp.jar
// Stops JIT compilation at tier 1, speeding up startup.9. Reduce Classpath Scanning
Limiting the packages scanned by Spring Boot cuts down initialization work.
@SpringBootApplication(scanBasePackages = "com.myapp.package1")
public class MyApplication {
// ...
}
// This restricts the packages Spring Boot scans at startup.10. Use Spring Boot DevTools Restart Feature
DevTools provides fast restart during development, improving developer productivity.
# application.properties
spring.devtools.restart.enabled=true
// Enables DevTools restart, allowing code changes without a full restart.By applying these methods—lazy loading, configuration tweaks, dependency pruning, bean optimization, JVM tuning, and development tools—you can noticeably reduce Spring Boot startup time and enhance overall development and deployment efficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
