Spring / Spring Boot 4 Basics Interview Questions
How does Spring Boot 4 support GraalVM native images?
Spring Boot 4 has first-class support for compiling applications to GraalVM native images -- ahead-of-time compiled binaries that start in milliseconds and use a fraction of the heap compared to JVM-based deployments. Boot 4 requires GraalVM native-image 25 or later.
# Requirements for native image in Spring Boot 4: # 1. GraalVM native-image 25 (or later) # 2. java.version >= 17 # 3. spring-boot-starter-parent as parent (or spring AOT plugin) # Build native image with Maven: ./mvnw -Pnative native:compile # Output: target/order-service (native binary) # Build native image with Gradle: ./gradlew nativeCompile # Output: build/native/nativeCompile/order-service # Build Docker image with native binary (Buildpacks): ./mvnw spring-boot:build-image -Pnative # Creates a container image using Cloud Native Buildpacks // Native image hints for reflection (when AOT analysis misses something): @ImportRuntimeHints(MyRuntimeHints.class) @SpringBootApplication public class App { ... } public class MyRuntimeHints implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, ClassLoader cl) { // Register class for reflection: hints.reflection() .registerType(MyDynamicClass.class, MemberCategory.INVOKE_DECLARED_METHODS); // Register resource file: hints.resources().registerPattern("templates/*.html"); } } # Native image startup comparison: # JVM mode: ~2-4 seconds startup, ~256MB heap # Native mode: ~50-100ms startup, ~50MB memory # Trade-off: Build time 5-10 minutes; no JIT optimisation at runtime
More Related questions...