[[aot]]
= 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 `org.springframework.boot.aot` Gradle plugin provides tasks that can be used to perform AOT processing on both application and test code.
The plugin is applied automatically when the {url-native-build-tools-docs-gradle-plugin}[GraalVM Native Image plugin] is applied:

[tabs]
======
Groovy::
+
[source,groovy,indent=0,subs="verbatim,attributes"]
----
include::example$aot/apply-native-image-plugin.gradle[]
----
Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,attributes"]
----
include::example$aot/apply-native-image-plugin.gradle.kts[]
----
======

If you want to  xref:reference:packaging/aot.adoc[run an AOT-processed application on the JVM] rather than as a native image, apply the `org.springframework.boot.aot` plugin directly:

[tabs]
======
Groovy::
+
[source,groovy,indent=0,subs="verbatim,attributes"]
----
include::example$aot/apply-aot-plugin.gradle[]
----
Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,attributes"]
----
include::example$aot/apply-aot-plugin.gradle.kts[]
----
======



[[aot.processing-applications]]
== 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 {url-gradle-dsl}/org.gradle.api.tasks.JavaExec.html[`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.



[[aot.processing-tests]]
== 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.
