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
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,9 @@ Applying such optimizations early implies the following restrictions:
17
17
* The beans defined in your application cannot change at runtime, meaning:
18
18
** `@Profile`, in particular profile-specific configuration, needs to be chosen at build time and is automatically enabled at runtime when AOT is enabled.
19
19
** `Environment` properties that impact the presence of a bean (`@Conditional`) are only considered at build time.
20
-
* Bean definitions with instance suppliers (lambdas or method references) cannot be transformed ahead-of-time.
20
+
* Bean definitions with instance suppliers (lambdas or method references) cannot be transformed ahead of time.
21
21
* Beans registered as singletons (using `registerSingleton`, typically from
22
-
`ConfigurableListableBeanFactory`) cannot be transformed ahead-of-time either.
22
+
`ConfigurableListableBeanFactory`) cannot be transformed ahead of time either.
23
23
* As we cannot rely on the instance, make sure that the bean type is as precise as
24
24
possible.
25
25
@@ -106,7 +106,7 @@ Consequently, such a bean is automatically excluded from the AOT-optimized conte
106
106
[NOTE]
107
107
====
108
108
If a bean implements the `BeanFactoryInitializationAotProcessor` interface, the bean and **all** of its dependencies will be initialized during AOT processing.
109
-
We generally recommend that this interface is only implemented by infrastructure beans such as `BeanFactoryPostProcessor` which have limited dependencies and are already initialized early in the bean factory lifecycle.
109
+
We generally recommend that this interface is only implemented by infrastructure beans, such as a `BeanFactoryPostProcessor`, which have limited dependencies and are already initialized early in the bean factory lifecycle.
110
110
If such a bean is registered using an `@Bean` factory method, ensure the method is `static` so that its enclosing `@Configuration` class does not have to be initialized.
111
111
====
112
112
@@ -127,7 +127,7 @@ Typically used when the bean definition needs to be tuned for specific features
127
127
[NOTE]
128
128
====
129
129
If a bean implements the `BeanRegistrationAotProcessor` interface, the bean and **all** of its dependencies will be initialized during AOT processing.
130
-
We generally recommend that this interface is only implemented by infrastructure beans such as `BeanFactoryPostProcessor` which have limited dependencies and are already initialized early in the bean factory lifecycle.
130
+
We generally recommend that this interface is only implemented by infrastructure beans, such as a `BeanFactoryPostProcessor`, which have limited dependencies and are already initialized early in the bean factory lifecycle.
131
131
If such a bean is registered using an `@Bean` factory method, ensure the method is `static` so that its enclosing `@Configuration` class does not have to be initialized.
132
132
====
133
133
@@ -219,7 +219,7 @@ NOTE: The exact code generated may differ depending on the exact nature of your
219
219
TIP: Each generated class is annotated with `org.springframework.aot.generate.Generated` to
220
220
identify them if they need to be excluded, for instance by static analysis tools.
221
221
222
-
The generated code above creates bean definitions equivalent to the `@Configuration` class, but in a direct way and without the use of reflection if at all possible.
222
+
The generated code above creates bean definitions equivalent to the `@Configuration` class, but in a direct way and without the use of reflection at all if possible.
223
223
There is a bean definition for `dataSourceConfiguration` and one for `dataSourceBean`.
224
224
When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
225
225
This supplier invokes the `dataSource()` method on the `dataSourceConfiguration` bean.
@@ -228,12 +228,12 @@ This supplier invokes the `dataSource()` method on the `dataSourceConfiguration`
228
228
== Running with AOT Optimizations
229
229
230
230
AOT is a mandatory step to transform a Spring application to a native executable, so it
231
-
is automatically enabled when running in this mode. It is possible to use those optimizations
231
+
is automatically enabled when running within a native image. However it is also possible to use AOT optimizations
232
232
on the JVM by setting the `spring.aot.enabled` System property to `true`.
233
233
234
-
NOTE: When AOT optimizations are included, some decisions that have been taken at build-time
235
-
are hard-coded in the application setup. For instance, profiles that have been enabled at
236
-
build-time are automatically enabled at runtime as well.
234
+
NOTE: When AOT optimizations are included, some decisions that have been made at buildtime
235
+
are hardcoded in the application setup. For instance, profiles that have been enabled at
236
+
buildtime are automatically enabled at runtime as well.
237
237
238
238
[[aot.bestpractices]]
239
239
== Best Practices
@@ -271,7 +271,7 @@ build time.
271
271
While your application may interact with an interface that a bean implements, it is still very important to declare the most precise type.
272
272
The AOT engine performs additional checks on the bean type, such as detecting the presence of `@Autowired` members or lifecycle callback methods.
273
273
274
-
For `@Configuration` classes, make sure that the return type of the factory `@Bean` method is as precise as possible.
274
+
For `@Configuration` classes, make sure that the return type of a `@Bean` factory method is as precise as possible.
275
275
Consider the following example:
276
276
277
277
[tabs]
@@ -305,11 +305,11 @@ Kotlin::
305
305
----
306
306
======
307
307
308
-
In the example above, the declared type for the `myInterface` bean is `MyInterface`.
309
-
None of the usual post-processing will take `MyImplementation` into account.
310
-
For instance, if there is an annotated handler method on `MyImplementation` that the context should register, it won’t be detected upfront.
308
+
In the example above, the declared type for the `myInterface` bean is `MyInterface`.
309
+
During AOT processing, none of the usual post-processing will take `MyImplementation` into account.
310
+
For instance, if there is an annotated handler method on `MyImplementation` that the context should register, it will not be detected during AOT processing.
311
311
312
-
The example above should be rewritten as follows:
312
+
The example above should therefore be rewritten as follows:
313
313
314
314
[tabs]
315
315
======
@@ -348,7 +348,7 @@ If you are registering bean definitions programmatically, consider using `RootBe
348
348
=== Avoid Multiple Constructors
349
349
350
350
The container is able to choose the most appropriate constructor to use based on several candidates.
351
-
However, this is not a best practice and flagging the preferred constructor with `@Autowired` if necessary is preferred.
351
+
However, relying on that is not a best practice, and flagging the preferred constructor with `@Autowired` if necessary is preferred.
352
352
353
353
In case you are working on a code base that you cannot modify, you can set the {spring-framework-api}/beans/factory/support/AbstractBeanDefinition.html#PREFERRED_CONSTRUCTORS_ATTRIBUTE[`preferredConstructors` attribute] on the related bean definition to indicate which constructor should be used.
354
354
@@ -363,13 +363,13 @@ A good rule of thumb is to keep in mind that bean definitions are an abstraction
363
363
Rather than using such structures, decomposing to simple types or referring to a bean that is built as such is recommended.
364
364
365
365
As a last resort, you can implement your own `org.springframework.aot.generate.ValueCodeGenerator$Delegate`.
366
-
To use it, register its fullyqualified name in `META-INF/spring/aot.factories` using the `Delegate` as the key.
366
+
To use it, register its fully-qualified name in `META-INF/spring/aot.factories` using `org.springframework.aot.generate.ValueCodeGenerator$Delegate` as the key.
367
367
368
368
[[aot.bestpractices.custom-arguments]]
369
369
=== Avoid Creating Beans with Custom Arguments
370
370
371
-
Spring AOT detects what needs to be done to create a bean and translates that in generated code using an instance supplier.
372
-
The container also supports creating a bean with {spring-framework-api}++/beans/factory/BeanFactory.html#getBean(java.lang.String,java.lang.Object...)++[custom arguments] that leads to several issues with AOT:
371
+
Spring AOT detects what needs to be done to create a bean and translates that into generated code that uses an instance supplier.
372
+
The container also supports creating a bean with {spring-framework-api}++/beans/factory/BeanFactory.html#getBean(java.lang.String,java.lang.Object...)++[custom arguments] which can lead to several issues with AOT:
373
373
374
374
. The custom arguments require dynamic introspection of a matching constructor or factory method.
375
375
Those arguments cannot be detected by AOT, so the necessary reflection hints will have to be provided manually.
@@ -396,7 +396,7 @@ for further information.
396
396
=== FactoryBean
397
397
398
398
`FactoryBean` should be used with care as it introduces an intermediate layer in terms of bean type resolution that may not be conceptually necessary.
399
-
As a rule of thumb, if the `FactoryBean` instance does not hold long-term state and is not needed at a later point in time at runtime, it should be replaced by a regular factory method, possibly with a `FactoryBean` adapter layer on top (for declarative configuration purposes).
399
+
As a rule of thumb, if a `FactoryBean` instance does not hold long-term state and is not needed at a later point at runtime, it should be replaced by a regular `@Bean` factory method, possibly with a `FactoryBean` adapter layer on top (for declarative configuration purposes).
400
400
401
401
If your `FactoryBean` implementation does not resolve the object type (i.e. `T`), extra care is necessary.
402
402
Consider the following example:
@@ -455,7 +455,7 @@ Kotlin::
455
455
----
456
456
======
457
457
458
-
If the `FactoryBean` bean definition is registered programmatically, make sure to follow these steps:
458
+
If a `FactoryBean` bean definition is registered programmatically, make sure to follow these steps:
459
459
460
460
1. Use `RootBeanDefinition`.
461
461
2. Set the `beanClass` to the `FactoryBean` class so that AOT knows that it is an intermediate layer.
@@ -520,7 +520,7 @@ Kotlin::
520
520
----
521
521
======
522
522
523
-
To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
523
+
To ensure that entity scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
524
524
factory bean definition, as shown by the following example:
525
525
526
526
[tabs]
@@ -609,7 +609,7 @@ Implementations of this interface can be registered using `@ImportRuntimeHints`
609
609
include-code::./SpellCheckService[]
610
610
611
611
If at all possible, `@ImportRuntimeHints` should be used as close as possible to the component that requires the hints.
612
-
This way, if the component is not contributed to the `BeanFactory`, the hints won't be contributed either.
612
+
This way, if the component is not contributed to the `BeanFactory`, the hints will not be contributed either.
613
613
614
614
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.
615
615
@@ -620,13 +620,13 @@ It is also possible to register an implementation statically by adding an entry
620
620
{spring-framework-api}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
621
621
For instance, `@EventListener` is meta-annotated with `@Reflective` since the underlying implementation invokes the annotated method using reflection.
622
622
623
-
Out-of-the-box, only Spring beans are considered but you can opt-in for scanning using `@ReflectiveScan`.
624
-
In the example below, all types of the package `com.example.app` and their subpackages are considered:
623
+
Out-of-the-box, only Spring beans are considered, but you can opt-in for scanning using `@ReflectiveScan`.
624
+
In the example below, all types in the `com.example.app` package and its subpackages are considered:
625
625
626
626
include-code::./MyConfiguration[]
627
627
628
-
Scanning happens during AOT processing and the types in the target packages do not need to have a class-level annotation to be considered.
629
-
This performs a "deep scan" and the presence of `@Reflective`, either directly or as a meta-annotation, is checked on types, fields, constructors, methods, and enclosed elements.
628
+
Scanning happens during AOT processing, and the types in the target packages do not need to have a class-level annotation to be considered.
629
+
This performs a _deep scan_, and the presence of `@Reflective`, either directly or as a meta-annotation, is checked on types, fields, constructors, methods, and enclosed elements.
630
630
631
631
By default, `@Reflective` registers an invocation hint for the annotated element.
632
632
This can be tuned by specifying a custom `ReflectiveProcessor` implementation via the `@Reflective` annotation.
@@ -640,7 +640,7 @@ An example of such customization is covered in the next section.
640
640
641
641
{spring-framework-api}/aot/hint/annotation/RegisterReflection.html[`@RegisterReflection`] is a specialization of `@Reflective` that provides a declarative way of registering reflection for arbitrary types.
642
642
643
-
NOTE: As a specialization of `@Reflective`, this is also detected if you're using `@ReflectiveScan`.
643
+
NOTE: As a specialization of `@Reflective`, this is also detected if you are using `@ReflectiveScan`.
644
644
645
645
In the following example, public constructors and public methods can be invoked via reflection on `AccountService`:
0 commit comments