|
| 1 | +[[aot]] |
| 2 | += Ahead-of-Time Processing |
| 3 | +Spring AOT is a process that analyzes your code at build-time in order to generate an optimized version of it. |
| 4 | +It is most often used to help generate GraalVM native images. |
| 5 | + |
| 6 | +The Spring Boot Gradle plugin provides tasks that can be used to perform AOT processing on both application and test code. |
| 7 | +The tasks are configured automatically when the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied: |
| 8 | + |
| 9 | +[source,groovy,indent=0,subs="verbatim,attributes",role="primary"] |
| 10 | +.Groovy |
| 11 | +---- |
| 12 | +include::../gradle/aot/apply-native-image-plugin.gradle[] |
| 13 | +---- |
| 14 | + |
| 15 | +[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"] |
| 16 | +.Kotlin |
| 17 | +---- |
| 18 | +include::../gradle/aot/apply-native-image-plugin.gradle.kts[] |
| 19 | +---- |
| 20 | + |
| 21 | + |
| 22 | +[[aot.processing-applications]] |
| 23 | +== Processing Applications |
| 24 | +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. |
| 25 | +Additional post-processing of the factory is possible using callbacks. |
| 26 | +For instance, these are used to generate the necessary reflection configuration that GraalVM needs to initialize the context in a native image. |
| 27 | + |
| 28 | +As the `BeanFactory` is fully prepared at build-time, conditions are also evaluated. |
| 29 | +This has an important difference compared to what a regular Spring Boot application does at runtime. |
| 30 | +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. |
| 31 | +To this end, the `processAot` task is a {gradle-dsl}/org.gradle.api.tasks.JavaExec.html[`JavaExec`] task and can be configured with environment variables, system properties, and arguments as needed. |
| 32 | + |
| 33 | +The `nativeCompile` task of the GraalVM Native Image plugin is automatically configured to use the output of the `processAot` task. |
| 34 | + |
| 35 | + |
| 36 | +[[aot.processing-tests]] |
| 37 | +== Processing Tests |
| 38 | +The AOT engine can be applied to JUnit 5 tests that use Spring's Test Context Framework. |
| 39 | +Suitable tests are processed by the `processTestAot` task to generate `ApplicationContextInitialzer` code. |
| 40 | +As with application AOT processing, the `BeanFactory` is fully prepared at build-time. |
| 41 | +As with `processAot`, the `processTestAot` task is `JavaExec` sub-class and can be configured as needed to influence this processing. |
| 42 | + |
| 43 | +The `nativeTest` task of the GraalVM Native Image plugin is automatically configured to use the output of the `processAot` and `processTestAot` tasks. |
0 commit comments