You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/core/aot.adoc
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
-
[[core.aot]]
1
+
[[aot]]
2
2
= Ahead of Time Optimizations
3
3
4
4
This chapter covers Spring's Ahead of Time (AOT) optimizations.
5
5
6
6
For AOT support specific to integration tests, see xref:testing/testcontext-framework/aot.adoc[Ahead of Time Support for Tests].
7
7
8
-
[[core.aot.introduction]]
8
+
[[aot.introduction]]
9
9
== Introduction to Ahead of Time Optimizations
10
10
11
11
Spring's support for AOT optimizations is meant to inspect an `ApplicationContext` at build time and apply decisions and discovery logic that usually happens at runtime.
@@ -30,7 +30,7 @@ A Spring AOT processed application typically generates:
30
30
NOTE: At the moment, AOT is focused on allowing Spring applications to be deployed as native images using GraalVM.
31
31
We intend to support more JVM-based use cases in future generations.
32
32
33
-
[[core.aot.basics]]
33
+
[[aot.basics]]
34
34
== AOT engine overview
35
35
36
36
The entry point of the AOT engine for processing an `ApplicationContext` arrangement is `ApplicationContextAotGenerator`. It takes care of the following steps, based on a `GenericApplicationContext` that represents the application to optimize and a {api-spring-framework}/aot/generate/GenerationContext.html[`GenerationContext`]:
@@ -46,21 +46,21 @@ The `RuntimeHints` instance can also be used to generate the relevant GraalVM na
46
46
47
47
Those steps are covered in greater detail in the sections below.
48
48
49
-
[[core.aot.refresh]]
49
+
[[aot.refresh]]
50
50
== Refresh for AOT Processing
51
51
52
52
Refresh for AOT processing is supported on all `GenericApplicationContext` implementations.
53
53
An application context is created with any number of entry points, usually in the form of `@Configuration`-annotated classes.
Starting this application with the regular runtime involves a number of steps including classpath scanning, configuration class parsing, bean instantiation, and lifecycle callback handling.
60
60
Refresh for AOT processing only applies a subset of what happens with a xref:core/beans/introduction.adoc[regular `refresh`].
In this mode, xref:core/beans/factory-extension.adoc#beans-factory-extension-factory-postprocessors[`BeanFactoryPostProcessor` implementations] are invoked as usual.
66
66
This includes configuration class parsing, import selectors, classpath scanning, etc.
@@ -76,7 +76,7 @@ This makes sure to create any proxy that will be required at runtime.
76
76
77
77
Once this part completes, the `BeanFactory` contains the bean definitions that are necessary for the application to run. It does not trigger bean instantiation but allows the AOT engine to inspect the beans that will be created at runtime.
Components that want to participate in this step can implement the {api-spring-framework}/beans/factory/aot/BeanFactoryInitializationAotProcessor.html[`BeanFactoryInitializationAotProcessor`] interface.
@@ -99,7 +99,7 @@ If such a bean is registered using an `@Bean` factory method, ensure the method
99
99
====
100
100
101
101
102
-
[[core.aot.bean-registration-contributions]]
102
+
[[aot.bean-registration-contributions]]
103
103
=== Bean Registration AOT Contributions
104
104
105
105
A core `BeanFactoryInitializationAotProcessor` implementation is responsible for collecting the necessary contributions for each candidate `BeanDefinition`.
@@ -186,7 +186,7 @@ When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
186
186
This supplier invokes the `dataSource()` method on the `dataSourceConfiguration` bean.
187
187
188
188
189
-
[[core.aot.hints]]
189
+
[[aot.hints]]
190
190
== Runtime Hints
191
191
192
192
Running an application as a native image requires additional information compared to a regular JVM runtime.
@@ -210,22 +210,22 @@ For cases that the core container cannot infer, you can register such hints prog
210
210
A number of convenient annotations are also provided for common use cases.
211
211
212
212
213
-
[[core.aot.hints.import-runtime-hints]]
213
+
[[aot.hints.import-runtime-hints]]
214
214
=== `@ImportRuntimeHints`
215
215
216
216
`RuntimeHintsRegistrar` implementations allow you to get a callback to the `RuntimeHints` instance managed by the AOT engine.
217
217
Implementations of this interface can be registered using `@ImportRuntimeHints` on any Spring bean or `@Bean` factory method.
218
218
`RuntimeHintsRegistrar` implementations are detected and invoked at build time.
219
219
220
-
include-code::SpellCheckService[]
220
+
include-code::./SpellCheckService[]
221
221
222
222
If at all possible, `@ImportRuntimeHints` should be used as close as possible to the component that requires the hints.
223
223
This way, if the component is not contributed to the `BeanFactory`, the hints won't be contributed either.
224
224
225
225
It is also possible to register an implementation statically by adding an entry in `META-INF/spring/aot.factories` with a key equal to the fully qualified name of the `RuntimeHintsRegistrar` interface.
226
226
227
227
228
-
[[core.aot.hints.reflective]]
228
+
[[aot.hints.reflective]]
229
229
=== `@Reflective`
230
230
231
231
{api-spring-framework}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
@@ -239,7 +239,7 @@ Library authors can reuse this annotation for their own purposes.
239
239
If components other than Spring beans need to be processed, a `BeanFactoryInitializationAotProcessor` can detect the relevant types and use `ReflectiveRuntimeHintsRegistrar` to process them.
{api-spring-framework}/aot/hint/annotation/RegisterReflectionForBinding.html[`@RegisterReflectionForBinding`] is a specialization of `@Reflective` that registers the need for serializing arbitrary types.
@@ -262,14 +262,14 @@ The following example registers `Account` for serialization.
262
262
}
263
263
----
264
264
265
-
[[core.aot.hints.testing]]
265
+
[[aot.hints.testing]]
266
266
=== Testing Runtime Hints
267
267
268
268
Spring Core also ships `RuntimeHintsPredicates`, a utility for checking that existing hints match a particular use case.
269
269
This can be used in your own tests to validate that a `RuntimeHintsRegistrar` contains the expected results.
270
270
We can write a test for our `SpellCheckService` and ensure that we will be able to load a dictionary at runtime:
With `RuntimeHintsPredicates`, we can check for reflection, resource, serialization, or proxy generation hints.
275
275
This approach works well for unit tests but implies that the runtime behavior of a component is well known.
@@ -281,11 +281,11 @@ For more targeted discovery and testing, Spring Framework ships a dedicated modu
281
281
This module contains the RuntimeHints Agent, a Java agent that records all method invocations that are related to runtime hints and helps you to assert that a given `RuntimeHints` instance covers all recorded invocations.
282
282
Let's consider a piece of infrastructure for which we'd like to test the hints we're contributing during the AOT processing phase.
283
283
284
-
include-code::SampleReflection[]
284
+
include-code::./SampleReflection[]
285
285
286
286
We can then write a unit test (no native compilation required) that checks our contributed hints:
0 commit comments