Spring Boot: From Basics to Production · Deployment and Performance
GraalVM Native Images
Compile Spring Boot apps ahead-of-time for fast startup and low memory.
Spring Boot supports GraalVM native images via AOT processing: ./mvnw -Pnative native:compile (or spring-boot:build-image -Pnative) produces a native executable that starts in tens of milliseconds and uses less memory — great for serverless, CLIs and scale-to-zero. Trade-offs: long build times, a closed-world assumption (reflection, proxies and resources need hints), and sometimes lower peak throughput than the JIT.
// Register reflection hints for classes used dynamically
@Configuration
@ImportRuntimeHints(ReportHints.class)
class ReportConfig {}
class ReportHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(ReportTemplate.class, MemberCategory.values());
hints.resources().registerPattern("templates/reports/*.html");
}
}
./mvnw -Pnative native:compile
./target/shop # starts in ~0.1s
Common pitfalls
- Libraries relying on runtime reflection without hints.
- Not testing the native build in CI (
nativeTest). - Choosing native for long-running high-throughput services without measuring.
Key points
- AOT + GraalVM for fast startup
- Runtime hints for reflection/resources
- Measure throughput trade-offs
Further reading: Spring Boot: Native images
Practice questions
When is a native image a good fit?
Answer
When startup time and memory matter more than peak throughput: serverless functions, scale-to-zero services, CLIs and dense deployments.
Why do native images need runtime hints?
Answer
GraalVM analyzes reachable code at build time (closed world); classes accessed via reflection, proxies or resources loaded dynamically must be declared or they won't be included.
Open in the app to mark lessons complete, take notes and track your progress.