Ahead-of-Time Processing

Spring AOT is a process that analyzes your code at build-time in order to generate an optimized version of it. It is most often used to help generate GraalVM native images.

The Spring Boot Gradle plugin provides tasks that can be used to perform AOT processing on both application and test code. The tasks are configured automatically when the GraalVM Native Image plugin is applied:

  • Groovy

  • Kotlin

plugins {
	id 'org.springframework.boot' version '3.3.0-SNAPSHOT'
	id 'org.graalvm.buildtools.native' version '0.10.1'
	id 'java'
}
plugins {
	id("org.springframework.boot") version "3.3.0-SNAPSHOT"
	id("org.graalvm.buildtools.native") version "0.10.1"
	java
}
Groovy

Kotlin

Processing Applications

Based on your @SpringBootApplication-annotated main class, the processAot task generates a persistent view of the beans that are going to be contributed at runtime in a way that bean instantiation is as straightforward as possible. Additional post-processing of the factory is possible using callbacks. For instance, these are used to generate the necessary reflection configuration that GraalVM needs to initialize the context in a native image.

As the BeanFactory is fully prepared at build-time, conditions are also evaluated. This has an important difference compared to what a regular Spring Boot application does at runtime. For instance, if you want to opt-in or opt-out for certain features, you need to configure the environment used at build time to do so. To this end, the processAot task is a JavaExec task and can be configured with environment variables, system properties, and arguments as needed.

The nativeCompile task of the GraalVM Native Image plugin is automatically configured to use the output of the processAot task.

Processing Tests

The AOT engine can be applied to JUnit 5 tests that use Spring’s Test Context Framework. Suitable tests are processed by the processTestAot task to generate ApplicationContextInitializer code. As with application AOT processing, the BeanFactory is fully prepared at build-time. As with processAot, the processTestAot task is JavaExec subclass and can be configured as needed to influence this processing.

The nativeTest task of the GraalVM Native Image plugin is automatically configured to use the output of the processAot and processTestAot tasks.