7 Proven Techniques to Slash Spring Boot Startup Time by Over 70%

This article walks through seven practical Spring Boot startup‑time optimizations—lazy initialization, precise component scanning, JVM tuning, auto‑configuration pruning, class‑loading reduction, lazy database connections, and AOT/layered compilation—backed by real‑world case studies and measurable performance gains.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
7 Proven Techniques to Slash Spring Boot Startup Time by Over 70%

This article presents a systematic guide to dramatically reduce Spring Boot application startup time using seven optimization techniques, with real‑world case studies and measurable results.

01 Lazy Initialization: On‑Demand Bean Loading

Enable lazy initialization by adding spring.main.lazy-initialization=true to application.properties. This delays the creation of all beans until they are first accessed, cutting I/O and dependency resolution during startup. Specific beans can be excluded with @Lazy(false).

# application.properties
spring.main.lazy-initialization=true
// Disable lazy init for a particular bean
@Bean
@Lazy(false)
public CriticalBean criticalBean() {
    return new CriticalBean();
}

Delays all bean initialization until first use.

Reduces I/O operations and dependency parsing at startup.

Effect: E‑commerce app startup reduced from 8.2 s to 5.1 s (‑38%); microservice gateway from 12 s to 7.3 s (‑39%).

02 Precise Component Scan

Limit the packages scanned by Spring Boot to only those that contain needed services and controllers, avoiding unnecessary classpath scanning.

@SpringBootApplication(
    scanBasePackages = {"com.your.package.service", "com.your.package.controller"}
)

Advanced: use @ComponentScan with excludeFilters to skip external libraries or specific annotations.

@ComponentScan(excludeFilters = {
    @Filter(type = FilterType.REGEX, pattern = "com.external.*"),
    @Filter(type = FilterType.ANNOTATION, classes = Repository.class)
})

Financial system excluded 20 unnecessary auto‑configuration classes, cutting startup from 6.5 s to 4.2 s (‑35%).

03 JVM Parameter Tuning

Apply a set of JVM flags that favor fast startup while keeping acceptable runtime performance.

java -XX:TieredStopAtLevel=1 \
     -Xverify:none \
     -XX:+AlwaysPreTouch \
     -XX:MetaspaceSize=128m \
     -XX:MaxMetaspaceSize=128m \
     -jar your-app.jar

Parameter table:

JVM parameter table
JVM parameter table

Result: Logistics system startup reduced from 9 s to 5.4 s (‑40%).

04 Auto‑Configuration Slimming

Print the auto‑configuration report and exclude unnecessary auto‑configuration classes.

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApp.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.setAdditionalProfiles("debug"); // print auto‑config report
        app.run(args);
    }
}
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class,
    RabbitAutoConfiguration.class
})

IoT platform excluded 15 auto‑config classes, cutting startup from 7.8 s to 4.6 s (‑41%).

05 Class‑Loading Optimization

Analyze class loading with java -verbose:class and remove unused dependencies. Enable Jar indexing to speed up classpath scanning.

# Use JDK tool
java -verbose:class -jar your-app.jar | grep "loaded"
<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>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <index>true</index>
        </archive>
    </configuration>
</plugin>

Social app class‑loading time dropped from 2.3 s to 1.1 s (‑52%).

06 Database Connection Optimization

Make the DataSource lazy and tune HikariCP pool parameters.

@Configuration
public class LazyDataSourceConfig {

    @Bean
    @Lazy
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
}
# HikariCP settings
spring.datasource.hikari.initialization-fail-timeout=30000
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=5

CRM system startup time reduced from 4.2 s to 1.3 s (‑69%).

07 Compilation Optimization: AOT & Layered Compilation

Build a native image with GraalVM for near‑instant startup.

# Install GraalVM native‑image tool
gu install native-image

# Build native image
mvn -Pnative package

API gateway startup dropped from 6 s to 0.05 s (‑99%).

Layered compilation strategy:

# Fast compile for dev
-Dspring.aot.enabled=false -XX:TieredStopAtLevel=1

# Full optimization for prod
-Dspring.aot.enabled=true -XX:TieredStopAtLevel=4

Payment service cold‑start reduced from 8 s to 2.3 s (‑71%).

08 Comprehensive Case: E‑commerce Platform

Before optimization: startup 14.6 s, memory 1.2 GB, class count 8,732.

Apply lazy init (‑3.2 s).

Precise component scan (‑2.8 s).

JVM tuning (‑1.9 s).

Exclude 12 auto‑config classes (‑2.1 s).

Trim dependencies (‑1.3 s).

Lazy DB connection (‑0.9 s).

Layered compilation (‑2.4 s).

After optimization: startup 4.3 s (‑70.5%), memory 680 MB (‑43%), class count 5,211 (‑40%).

09 Startup Optimization Checklist

Enable lazy initialization.

Configure component scan precisely.

Optimize JVM startup parameters.

Exclude unnecessary auto‑configuration.

Analyze and trim dependencies.

Delay non‑critical resource connections.

Consider AOT or layered compilation.

10 Effect Comparison Charts

Optimization effect comparison
Optimization effect comparison

By applying these seven tactics, a Spring Boot application can achieve more than 70 % reduction in startup time, though continuous monitoring and adjustment are essential for each specific project.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JVMstartup optimizationperformance tuningSpring BootAoTauto-configurationlazy-initialization
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.