Skip to content

Commit 95ad25e

Browse files
committed
Polish AOT section of the reference docs
1 parent 9670388 commit 95ad25e

File tree

1 file changed

+27
-27
lines changed
  • framework-docs/modules/ROOT/pages/core

1 file changed

+27
-27
lines changed

framework-docs/modules/ROOT/pages/core/aot.adoc

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Applying such optimizations early implies the following restrictions:
1717
* The beans defined in your application cannot change at runtime, meaning:
1818
** `@Profile`, in particular profile-specific configuration, needs to be chosen at build time and is automatically enabled at runtime when AOT is enabled.
1919
** `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.
2121
* 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.
2323
* As we cannot rely on the instance, make sure that the bean type is as precise as
2424
possible.
2525

@@ -106,7 +106,7 @@ Consequently, such a bean is automatically excluded from the AOT-optimized conte
106106
[NOTE]
107107
====
108108
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.
110110
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.
111111
====
112112

@@ -127,7 +127,7 @@ Typically used when the bean definition needs to be tuned for specific features
127127
[NOTE]
128128
====
129129
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.
131131
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.
132132
====
133133

@@ -219,7 +219,7 @@ NOTE: The exact code generated may differ depending on the exact nature of your
219219
TIP: Each generated class is annotated with `org.springframework.aot.generate.Generated` to
220220
identify them if they need to be excluded, for instance by static analysis tools.
221221

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.
223223
There is a bean definition for `dataSourceConfiguration` and one for `dataSourceBean`.
224224
When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
225225
This supplier invokes the `dataSource()` method on the `dataSourceConfiguration` bean.
@@ -228,12 +228,12 @@ This supplier invokes the `dataSource()` method on the `dataSourceConfiguration`
228228
== Running with AOT Optimizations
229229

230230
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
232232
on the JVM by setting the `spring.aot.enabled` System property to `true`.
233233

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 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.
237237

238238
[[aot.bestpractices]]
239239
== Best Practices
@@ -271,7 +271,7 @@ build time.
271271
While your application may interact with an interface that a bean implements, it is still very important to declare the most precise type.
272272
The AOT engine performs additional checks on the bean type, such as detecting the presence of `@Autowired` members or lifecycle callback methods.
273273

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.
275275
Consider the following example:
276276

277277
[tabs]
@@ -305,11 +305,11 @@ Kotlin::
305305
----
306306
======
307307

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.
311311

312-
The example above should be rewritten as follows:
312+
The example above should therefore be rewritten as follows:
313313

314314
[tabs]
315315
======
@@ -348,7 +348,7 @@ If you are registering bean definitions programmatically, consider using `RootBe
348348
=== Avoid Multiple Constructors
349349

350350
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.
352352

353353
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.
354354

@@ -363,13 +363,13 @@ A good rule of thumb is to keep in mind that bean definitions are an abstraction
363363
Rather than using such structures, decomposing to simple types or referring to a bean that is built as such is recommended.
364364

365365
As a last resort, you can implement your own `org.springframework.aot.generate.ValueCodeGenerator$Delegate`.
366-
To use it, register its fully qualified 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.
367367

368368
[[aot.bestpractices.custom-arguments]]
369369
=== Avoid Creating Beans with Custom Arguments
370370

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:
373373

374374
. The custom arguments require dynamic introspection of a matching constructor or factory method.
375375
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.
396396
=== FactoryBean
397397

398398
`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).
400400

401401
If your `FactoryBean` implementation does not resolve the object type (i.e. `T`), extra care is necessary.
402402
Consider the following example:
@@ -455,7 +455,7 @@ Kotlin::
455455
----
456456
======
457457

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:
459459

460460
1. Use `RootBeanDefinition`.
461461
2. Set the `beanClass` to the `FactoryBean` class so that AOT knows that it is an intermediate layer.
@@ -520,7 +520,7 @@ Kotlin::
520520
----
521521
======
522522

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
524524
factory bean definition, as shown by the following example:
525525

526526
[tabs]
@@ -609,7 +609,7 @@ Implementations of this interface can be registered using `@ImportRuntimeHints`
609609
include-code::./SpellCheckService[]
610610

611611
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.
613613

614614
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.
615615

@@ -620,13 +620,13 @@ It is also possible to register an implementation statically by adding an entry
620620
{spring-framework-api}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
621621
For instance, `@EventListener` is meta-annotated with `@Reflective` since the underlying implementation invokes the annotated method using reflection.
622622

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:
625625

626626
include-code::./MyConfiguration[]
627627

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.
630630

631631
By default, `@Reflective` registers an invocation hint for the annotated element.
632632
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.
640640

641641
{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.
642642

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`.
644644

645645
In the following example, public constructors and public methods can be invoked via reflection on `AccountService`:
646646

0 commit comments

Comments
 (0)