9 Proven Ways to Speed Up Spring Boot 3 Startup Time
This article explains nine practical techniques—including narrowing component scans, enabling lazy initialization, excluding unused auto‑configurations, using a component indexer, tuning the database pool, avoiding heavy bean init tasks, leveraging CDS, running exploded jars, and employing GraalVM native images—to dramatically reduce Spring Boot 3 application startup time.
1. Introduction
Spring Boot applications are convenient but startup time can become a bottleneck as projects grow. Understanding the internal startup phases—component scanning, bean instantiation, auto‑configuration, and dependency injection—allows targeted optimizations.
2. Practical Cases
2.1 Limit Component Scan Scope
By default @ComponentScan scans the package of the main class and its sub‑packages. For large projects, restrict scanning to necessary packages:
@SpringBootApplication
@ComponentScan(basePackages = {"com.pack.xxx.core", "com.pack.xxx.web", "com.pack.xxx.config"})
public class XxxApp {
public static void main(String[] args) {
SpringApplication.run(XxxApp.class, args);
}
}This reduces the number of classes Spring must examine.
2.2 Enable Lazy Initialization
Spring creates all singleton beans at startup. Use @Lazy or global lazy initialization to defer bean creation until first use.
Method 1 – annotate beans:
@Component
@Lazy
public class ComplexComponent {}
@Configuration
public class AppConfig {
@Bean
@Lazy
ComplexComponent cc() {
return new ComplexComponent();
}
}Method 2 – programmatic global setting:
@SpringBootApplication
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class)
.lazyInitialization(true)
.run(args);
}
}Method 3 – application.yml:
spring:
main:
lazy-initialization: trueNote: Lazy initialization may hide errors until runtime.
2.3 Exclude Unused Auto‑Configuration
Exclude auto‑config classes you do not use, e.g., DataSourceAutoConfiguration or SecurityAutoConfiguration:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class})
public class XxxApp {}Or via YAML:
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration2.4 Use Component Indexer
Add spring-context-indexer dependency to generate an index of beans, allowing Spring to locate components faster.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
</dependency>After packaging, an index file appears under META-INF. (Note: the indexer is deprecated in Spring 6.)
The Spring Context Indexer is being superseded by AOT compilation; it is no longer recommended.
2.5 Optimize Database Connection Pool Initialization
Configure HikariCP to use a small initial pool and lazy connection creation:
spring:
datasource:
hikari:
minimumIdle: 10
maximumPoolSize: 100
autoCommit: true
idleTimeout: 30000
poolName: PackHikariCP
maxLifetime: 1800000
connectionTimeout: 30000
connectionTestQuery: SELECT 1Be aware that too small a minimumIdle may cause latency under sudden load.
2.6 Avoid Time‑Consuming Tasks in Bean Initialization
Move heavy work from @PostConstruct or InitializingBean to asynchronous execution:
@Service
public class CacheLoader {
private final AsyncTaskExecutor taskExecutor;
public CacheLoader(AsyncTaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
@PostConstruct
public void init() {
taskExecutor.execute(this::fromRedisLoader);
}
private void fromRedisLoader() {
// time‑consuming task
}
}2.7 Use Class Data Sharing (CDS)
CDS reduces JVM startup time and memory. Create a CDS archive for the application classpath:
java -Djarmode=tools -jar app.jar extract --destination app
# then create archive
java -XX:ArchiveClassesAtExit=app.jsa -Dspring.context.exit=onRefresh -jar app.jar
# run with
java -XX:SharedArchiveFile=app.jsa -jar app.jar2.8 Run an Exploded Executable Jar
Running the application from an extracted directory avoids the overhead of nested JAR loading. Use:
java -Djarmode=tools -jar app.jar extract
java -jar app/app.jar2.9 GraalVM Native Image
Refer to the linked article for generating a native executable with GraalVM, achieving sub‑second startup and low memory usage.
These nine techniques collectively help reduce Spring Boot 3 startup time.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
