Skip to content

Commit 4454b3b

Browse files
committed
Polishing
1 parent 2dd22f6 commit 4454b3b

File tree

1 file changed

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

1 file changed

+25
-22
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ 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.
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
2222
`ConfigurableListableBeanFactory`) cannot be transformed ahead-of-time either.
23-
* As we can't rely on the instance, make sure that the bean type is as precise as
23+
* As we cannot rely on the instance, make sure that the bean type is as precise as
2424
possible.
2525

2626
TIP: See also the xref:core/aot.adoc#aot.bestpractices[] section.
@@ -30,7 +30,7 @@ A Spring AOT processed application typically generates:
3030

3131
* Java source code
3232
* Bytecode (usually for dynamic proxies)
33-
* {spring-framework-api}/aot/hint/RuntimeHints.html[`RuntimeHints`] for the use of reflection, resource loading, serialization, and JDK proxies.
33+
* {spring-framework-api}/aot/hint/RuntimeHints.html[`RuntimeHints`] for the use of reflection, resource loading, serialization, and JDK proxies
3434

3535
NOTE: At the moment, AOT is focused on allowing Spring applications to be deployed as native images using GraalVM.
3636
We intend to support more JVM-based use cases in future generations.
@@ -70,13 +70,14 @@ include-code::./AotProcessingSample[tag=aotcontext]
7070
In this mode, xref:core/beans/factory-extension.adoc#beans-factory-extension-factory-postprocessors[`BeanFactoryPostProcessor` implementations] are invoked as usual.
7171
This includes configuration class parsing, import selectors, classpath scanning, etc.
7272
Such steps make sure that the `BeanRegistry` contains the relevant bean definitions for the application.
73-
If bean definitions are guarded by conditions (such as `@Profile`), these are evaluated
73+
If bean definitions are guarded by conditions (such as `@Profile`), these are evaluated,
7474
and bean definitions that don't match their conditions are discarded at this stage.
7575

76-
If custom code needs to register extra beans programmatically, make sure that they use
77-
`BeanDefinitionRegistry`, and not `BeanFactory` as only bean definitions are taken into
78-
account. A good pattern is to implement `ImportBeanDefinitionRegistrar` and register it
79-
via an `@Import` on one of your configuration classes.
76+
If custom code needs to register extra beans programmatically, make sure that custom
77+
registration code uses `BeanDefinitionRegistry` instead of `BeanFactory` as only bean
78+
definitions are taken into account. A good pattern is to implement
79+
`ImportBeanDefinitionRegistrar` and register it via an `@Import` on one of your
80+
configuration classes.
8081

8182
Because this mode does not actually create bean instances, `BeanPostProcessor` implementations are not invoked, except for specific variants that are relevant for AOT processing.
8283
These are:
@@ -93,12 +94,12 @@ Once this part completes, the `BeanFactory` contains the bean definitions that a
9394
Components that want to participate in this step can implement the {spring-framework-api}/beans/factory/aot/BeanFactoryInitializationAotProcessor.html[`BeanFactoryInitializationAotProcessor`] interface.
9495
Each implementation can return an AOT contribution, based on the state of the bean factory.
9596

96-
An AOT contribution is a component that contributes generated code that reproduces a particular behavior.
97+
An AOT contribution is a component that contributes generated code which reproduces a particular behavior.
9798
It can also contribute `RuntimeHints` to indicate the need for reflection, resource loading, serialization, or JDK proxies.
9899

99-
A `BeanFactoryInitializationAotProcessor` implementation can be registered in `META-INF/spring/aot.factories` with a key equal to the fully qualified name of the interface.
100+
A `BeanFactoryInitializationAotProcessor` implementation can be registered in `META-INF/spring/aot.factories` with a key equal to the fully-qualified name of the interface.
100101

101-
A `BeanFactoryInitializationAotProcessor` can also be implemented directly by a bean.
102+
The `BeanFactoryInitializationAotProcessor` interface can also be implemented directly by a bean.
102103
In this mode, the bean provides an AOT contribution equivalent to the feature it provides with a regular runtime.
103104
Consequently, such a bean is automatically excluded from the AOT-optimized context.
104105

@@ -120,7 +121,7 @@ This interface is used as follows:
120121

121122
* Implemented by a `BeanPostProcessor` bean, to replace its runtime behavior.
122123
For instance xref:core/beans/factory-extension.adoc#beans-factory-extension-bpp-examples-aabpp[`AutowiredAnnotationBeanPostProcessor`] implements this interface to generate code that injects members annotated with `@Autowired`.
123-
* Implemented by a type registered in `META-INF/spring/aot.factories` with a key equal to the fully qualified name of the interface.
124+
* Implemented by a type registered in `META-INF/spring/aot.factories` with a key equal to the fully-qualified name of the interface.
124125
Typically used when the bean definition needs to be tuned for specific features of the core framework.
125126

126127
[NOTE]
@@ -218,30 +219,31 @@ This section lists the best practices that make sure your application is ready f
218219

219220
[[aot.bestpractices.bean-registration]]
220221
== Programmatic bean registration
221-
The AOT engine takes care of the `@Configuration` model, and any callback that might be
222+
223+
The AOT engine takes care of the `@Configuration` model and any callback that might be
222224
invoked as part of processing your configuration. If you need to register additional
223225
beans programmatically, make sure to use a `BeanDefinitionRegistry` to register
224226
bean definitions.
225227

226-
This can be typically done via a `BeanDefinitionRegistryPostProcessor`. Note that, if it
228+
This can typically be done via a `BeanDefinitionRegistryPostProcessor`. Note that, if it
227229
is registered itself as a bean, it will be invoked again at runtime unless you make
228230
sure to implement `BeanFactoryInitializationAotProcessor` as well. A more idiomatic
229231
way is to implement `ImportBeanDefinitionRegistrar` and register it using `@Import` on
230232
one of your configuration classes. This invokes your custom code as part of configuration
231233
class parsing.
232234

233-
If you declare additional beans programmatically using a different callback, there are
235+
If you declare additional beans programmatically using a different callback, they are
234236
likely not going to be handled by the AOT engine, and therefore no hints are going to be
235237
generated for them. Depending on the environment, those beans may not be registered at
236238
all. For instance, classpath scanning does not work in a native image as there is no
237-
notion of classpath. For cases like this, it is crucial that the scanning happens at
239+
notion of a classpath. For cases like this, it is crucial that the scanning happens at
238240
build time.
239241

240242
[[aot.bestpractices.bean-type]]
241243
=== Expose The Most Precise Bean Type
242244

243245
While your application may interact with an interface that a bean implements, it is still very important to declare the most precise type.
244-
The AOT engine performs additional checks on the bean type, such as detecting the presence of `@Autowired` members, or lifecycle callback methods.
246+
The AOT engine performs additional checks on the bean type, such as detecting the presence of `@Autowired` members or lifecycle callback methods.
245247

246248
For `@Configuration` classes, make sure that the return type of the factory `@Bean` method is as precise as possible.
247249
Consider the following example:
@@ -292,10 +294,11 @@ If you are registering bean definitions programmatically, consider using `RootBe
292294

293295
[[aot.bestpractices.constructors]]
294296
=== Avoid Multiple Constructors
297+
295298
The container is able to choose the most appropriate constructor to use based on several candidates.
296299
However, this is not a best practice and flagging the preferred constructor with `@Autowired` if necessary is preferred.
297300

298-
In case you are working on a code base that you can't 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.
301+
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.
299302

300303
[[aot.bestpractices.factory-bean]]
301304
=== FactoryBean
@@ -313,7 +316,7 @@ Java::
313316
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
314317
----
315318
public class ClientFactoryBean<T extends AbstractClient> implements FactoryBean<T> {
316-
319+
// ...
317320
}
318321
----
319322
======
@@ -410,7 +413,7 @@ Java::
410413

411414
Running an application as a native image requires additional information compared to a regular JVM runtime.
412415
For instance, GraalVM needs to know ahead of time if a component uses reflection.
413-
Similarly, classpath resources are not shipped in a native image unless specified explicitly.
416+
Similarly, classpath resources are not included in a native image unless specified explicitly.
414417
Consequently, if the application needs to load a resource, it must be referenced from the corresponding GraalVM native image configuration file.
415418

416419
The {spring-framework-api}/aot/hint/RuntimeHints.html[`RuntimeHints`] API collects the need for reflection, resource loading, serialization, and JDK proxies at runtime.
@@ -445,7 +448,7 @@ include-code::./SpellCheckService[]
445448
If at all possible, `@ImportRuntimeHints` should be used as close as possible to the component that requires the hints.
446449
This way, if the component is not contributed to the `BeanFactory`, the hints won't be contributed either.
447450

448-
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.
451+
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.
449452

450453

451454
[[aot.hints.reflective]]
@@ -454,7 +457,7 @@ It is also possible to register an implementation statically by adding an entry
454457
{spring-framework-api}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
455458
For instance, `@EventListener` is meta-annotated with `@Reflective` since the underlying implementation invokes the annotated method using reflection.
456459

457-
By default, only Spring beans are considered and an invocation hint is registered for the annotated element.
460+
By default, only Spring beans are considered, and an invocation hint is registered for the annotated element.
458461
This can be tuned by specifying a custom `ReflectiveProcessor` implementation via the
459462
`@Reflective` annotation.
460463

0 commit comments

Comments
 (0)