7 Proven Techniques to Slash Spring Boot Startup Time by Up to 70%
This article walks through seven concrete Spring Boot startup optimizations—including lazy initialization, precise component scanning, JVM tuning, auto‑configuration slimming, class‑loading reduction, delayed database connections, and AOT/native compilation—showing real‑world benchmarks that cut launch times by around 70%.
Overview
The author presents seven practical techniques to dramatically reduce Spring Boot startup time, each backed by code snippets, configuration examples, and measured performance improvements.
1. Lazy Initialization
Practice:
# application.properties
spring.main.lazy-initialization=truePrinciple: Delays bean creation until first use, reducing I/O and dependency resolution during startup.
Note: Specific beans can be excluded with @Bean and @Lazy(false):
// Disable lazy init for a critical bean
@Bean
@Lazy(false)
public CriticalBean criticalBean() {
return new CriticalBean();
}Effect:
E‑commerce app: startup 8.2 s → 5.1 s (‑38%)
Microservice gateway: 12 s → 7.3 s (‑39%)
2. Precise Component Scanning
Practice:
@SpringBootApplication(
scanBasePackages = {"com.your.package.service", "com.your.package.controller"}
)Advanced tip: Use @ComponentScan with excludeFilters to skip unwanted packages or annotations.
// Exclude external packages and repository annotations
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.REGEX, pattern = "com.external.*"),
@Filter(type = FilterType.ANNOTATION, classes = Repository.class)
})Typical case: A financial system removed 20 unnecessary auto‑configuration classes, reducing startup from 6.5 s to 4.2 s (‑35%).
3. JVM Parameter Tuning
Recommended combination:
java -XX:TieredStopAtLevel=1 \
-Xverify:none \
-XX:+AlwaysPreTouch \
-XX:MetaspaceSize=128m \
-XX:MaxMetaspaceSize=128m \
-jar your-app.jarParameter purpose: -XX:TieredStopAtLevel=1 – disables C2 compilation (development environment) -Xverify:none / -noverify – skips bytecode verification (trusted environment, JDK 8‑) -XX:+AlwaysPreTouch – pre‑allocates memory at startup (production) -XX:MetaspaceSize=128m – avoids frequent metaspace expansion (metadata‑heavy apps)
Measured impact: Logistics system startup 9 s → 5.4 s (‑40%).
4. Auto‑Configuration Slimming
Diagnostic tool: Enable the auto‑configuration report.
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setBannerMode(Banner.Mode.OFF);
// Print auto‑configuration report
app.setAdditionalProfiles("debug");
app.run(args);
}
}Exclude unnecessary auto‑config classes:
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
RabbitAutoConfiguration.class
})Case study: IoT platform excluded 15 auto‑configuration classes, cutting startup from 7.8 s to 4.6 s (‑41%).
5. Class‑Loading Optimization
Analysis tool: java -verbose:class -jar your-app.jar | grep "loaded" Strategy: Trim dependencies and enable Jar indexing.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>Enable Jar index in Maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<index>true</index>
</archive>
</configuration>
</plugin>Result: Social app class‑loading time 2.3 s → 1.1 s (‑52%).
6. Database Connection Optimization
Delay DB connection:
@Configuration
public class LazyDataSourceConfig {
@Bean
@Lazy
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}Connection‑pool tuning (HikariCP):
# HikariCP settings
spring.datasource.hikari.initialization-fail-timeout=30000
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=5Special handling: Use @PostConstruct or CommandLineRunner to run data initialization after startup.
Case: CRM system DB‑related startup time 4.2 s → 1.3 s (‑69%).
7. Compilation Optimization
7.1 GraalVM Native Image
# Install GraalVM native‑image tool
gu install native-image
# Build native image
mvn -Pnative packageEffect: API gateway startup 6 s → 0.05 s (‑99%).
7.2 Layered Compilation Strategy
# Fast compilation for development
-Dspring.aot.enabled=false -XX:TieredStopAtLevel=1
# Full optimization for production
-Dspring.aot.enabled=true -XX:TieredStopAtLevel=4Before/after: Payment service cold start 8 s → 2.3 s (‑71%).
Comprehensive Case: E‑Commerce Platform
Initial state:
Startup time: 14.6 s
Memory usage: 1.2 GB
Loaded classes: 8,732
Implementation steps (and time saved):
Enable lazy initialization (‑3.2 s)
Fine‑tune component scan (‑2.8 s)
Optimize JVM parameters (‑1.9 s)
Exclude 12 auto‑configuration classes (‑2.1 s)
Trim dependencies (‑1.3 s)
Delay DB connections (‑0.9 s)
Apply layered compilation (‑2.4 s)
Result after optimization:
Startup time: 4.3 s (‑70.5%)
Memory usage: 680 MB (‑43%)
Loaded classes: 5,211 (‑40%)
Startup Optimization Checklist
Enable lazy initialization
Precisely configure component‑scan range
Optimize JVM startup parameters
Exclude unnecessary auto‑configuration
Analyze and trim dependencies
Delay non‑critical resource connections
Consider AOT or layered compilation
By applying these seven “devil‑ish” practices, a Spring Boot application can achieve more than a 70% reduction in startup time, though continuous profiling and adjustment are required for each specific project.
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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
