Ahead-of-time (AOT) processing lets Spring Boot analyze and optimize your application at build time rather than at startup. This is a prerequisite for compiling to a GraalVM native image, and it also makes AOT-generated code available on the JVM for faster startup validation. This guide covers the most common tasks you will encounter when adopting AOT in a Spring Boot project.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
How AOT processing works
During the build, the Spring AOT engine processes the application context, evaluates@Conditional annotations against the build-time environment, and generates source code and configuration metadata. That generated code is included in the final artifact alongside the rest of your application.
Conditions evaluated at build time are fixed for the resulting artifact. Beans created under a condition that was true at AOT processing time are always created when the application runs, regardless of the runtime environment. Profile-based configuration that only changes property values (not bean creation) still works at runtime.
Enable AOT processing in your build
- Maven
- Gradle
Use Run AOT processing explicitly with the
spring-boot-starter-parent and add the native-maven-plugin. The parent POM defines a native profile that wires the process-aot goal automatically:pom.xml
native profile active:Set active profiles during AOT processing
Because@Profile-based conditions are evaluated at build time, you must tell the AOT engine which profiles should be active. Beans that depend on a profile must be created during processing for them to be available at runtime.
- Maven
- Gradle
Configure the
process-aot execution inside the native profile:pom.xml
Provide reflection hints with RuntimeHintsRegistrar
GraalVM native images do not support Java reflection unless explicit hints tell the compiler which classes, methods, and fields need to be accessible. Spring Boot auto-generates most hints, but you may need to add your own for third-party libraries or dynamic code patterns.Implement RuntimeHintsRegistrar
Create a class that implements
RuntimeHintsRegistrar and registers the hints you need:MyRuntimeHints.java
Provide resource hints
Resources loaded from the classpath at runtime must be declared explicitly for native images. Register them through theRuntimeHints API inside your RuntimeHintsRegistrar:
Spring Boot automatically registers hints for standard Spring resources such as
application.properties, Hibernate mapping files, and Spring XML configuration files. You only need to register resources that your own code loads dynamically.Use @RegisterReflectionForBinding for JSON serialization
When you serialize or deserialize types throughRestTemplate, RestClient, or WebClient rather than through a @RestController return type, Spring cannot infer the required reflection hints automatically. Annotate the bean that performs the call with @RegisterReflectionForBinding:
MyService.java
Test AOT-processed applications on the JVM
Compiling a native image takes several minutes. Before doing a full native build, validate your AOT-generated code on the JVM by enabling AOT mode at startup:The JAR you test must include AOT-generated assets. Build with
./mvnw -Pnative package (Maven) or ensure the org.graalvm.buildtools.native plugin is applied (Gradle) so that AOT processing runs during the build.spring.aot.enabled=true, you can have high confidence it will work as a native image. You can then run your regular integration tests against the running JVM process before committing to a full native compile.
Test in native image mode
GraalVM Native Build Tools supports running your entire test suite inside a native image. This is slower than a JVM test run, but catches any hint gaps that only appear in native mode. Use it in CI pipelines rather than in inner-loop development.- Maven
- Gradle
The
spring-boot-starter-parent defines a nativeTest profile. Activate it to build and run tests natively:Debug AOT hint issues
Use RuntimeHintsPredicates in unit tests
Use RuntimeHintsPredicates in unit tests
Test that your
RuntimeHintsRegistrar registers the hints you expect without running a full native build:MyRuntimeHintsTests.java
Use the GraalVM tracing agent
Use the GraalVM tracing agent
Run the application with the tracing agent to capture all reflection and resource accesses at runtime:Exercise the features that failed, then stop the application. Copy the generated JSON hint files into
src/main/resources/META-INF/native-image/ so GraalVM picks them up in the next build.Check GraalVM reachability metadata
Check GraalVM reachability metadata
If a third-party library does not work in native mode, check the GraalVM reachability metadata repository before writing your own hints. Many popular libraries already have community-contributed metadata that Spring Boot includes automatically via the
native-maven-plugin or org.graalvm.buildtools.native Gradle plugin.