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
In this mode, xref:core/beans/factory-extension.adoc#beans-factory-extension-factory-postprocessors[`BeanFactoryPostProcessor` implementations] are invoked as usual.
71
71
This includes configuration class parsing, import selectors, classpath scanning, etc.
72
72
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,
74
74
and bean definitions that don't match their conditions are discarded at this stage.
75
75
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.
80
81
81
82
Because this mode does not actually create bean instances, `BeanPostProcessor` implementations are not invoked, except for specific variants that are relevant for AOT processing.
82
83
These are:
@@ -93,12 +94,12 @@ Once this part completes, the `BeanFactory` contains the bean definitions that a
93
94
Components that want to participate in this step can implement the {spring-framework-api}/beans/factory/aot/BeanFactoryInitializationAotProcessor.html[`BeanFactoryInitializationAotProcessor`] interface.
94
95
Each implementation can return an AOT contribution, based on the state of the bean factory.
95
96
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.
97
98
It can also contribute `RuntimeHints` to indicate the need for reflection, resource loading, serialization, or JDK proxies.
98
99
99
-
A `BeanFactoryInitializationAotProcessor` implementation can be registered in `META-INF/spring/aot.factories` with a key equal to the fullyqualified 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.
100
101
101
-
A `BeanFactoryInitializationAotProcessor` can also be implemented directly by a bean.
102
+
The `BeanFactoryInitializationAotProcessor` interface can also be implemented directly by a bean.
102
103
In this mode, the bean provides an AOT contribution equivalent to the feature it provides with a regular runtime.
103
104
Consequently, such a bean is automatically excluded from the AOT-optimized context.
104
105
@@ -120,7 +121,7 @@ This interface is used as follows:
120
121
121
122
* Implemented by a `BeanPostProcessor` bean, to replace its runtime behavior.
122
123
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 fullyqualified 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.
124
125
Typically used when the bean definition needs to be tuned for specific features of the core framework.
125
126
126
127
[NOTE]
@@ -218,30 +219,31 @@ This section lists the best practices that make sure your application is ready f
218
219
219
220
[[aot.bestpractices.bean-registration]]
220
221
== 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
222
224
invoked as part of processing your configuration. If you need to register additional
223
225
beans programmatically, make sure to use a `BeanDefinitionRegistry` to register
224
226
bean definitions.
225
227
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
227
229
is registered itself as a bean, it will be invoked again at runtime unless you make
228
230
sure to implement `BeanFactoryInitializationAotProcessor` as well. A more idiomatic
229
231
way is to implement `ImportBeanDefinitionRegistrar` and register it using `@Import` on
230
232
one of your configuration classes. This invokes your custom code as part of configuration
231
233
class parsing.
232
234
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
234
236
likely not going to be handled by the AOT engine, and therefore no hints are going to be
235
237
generated for them. Depending on the environment, those beans may not be registered at
236
238
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
238
240
build time.
239
241
240
242
[[aot.bestpractices.bean-type]]
241
243
=== Expose The Most Precise Bean Type
242
244
243
245
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.
245
247
246
248
For `@Configuration` classes, make sure that the return type of the factory `@Bean` method is as precise as possible.
247
249
Consider the following example:
@@ -292,10 +294,11 @@ If you are registering bean definitions programmatically, consider using `RootBe
292
294
293
295
[[aot.bestpractices.constructors]]
294
296
=== Avoid Multiple Constructors
297
+
295
298
The container is able to choose the most appropriate constructor to use based on several candidates.
296
299
However, this is not a best practice and flagging the preferred constructor with `@Autowired` if necessary is preferred.
297
300
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.
public class ClientFactoryBean<T extends AbstractClient> implements FactoryBean<T> {
316
-
319
+
// ...
317
320
}
318
321
----
319
322
======
@@ -410,7 +413,7 @@ Java::
410
413
411
414
Running an application as a native image requires additional information compared to a regular JVM runtime.
412
415
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.
414
417
Consequently, if the application needs to load a resource, it must be referenced from the corresponding GraalVM native image configuration file.
415
418
416
419
The {spring-framework-api}/aot/hint/RuntimeHints.html[`RuntimeHints`] API collects the need for reflection, resource loading, serialization, and JDK proxies at runtime.
If at all possible, `@ImportRuntimeHints` should be used as close as possible to the component that requires the hints.
446
449
This way, if the component is not contributed to the `BeanFactory`, the hints won't be contributed either.
447
450
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 fullyqualified 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.
449
452
450
453
451
454
[[aot.hints.reflective]]
@@ -454,7 +457,7 @@ It is also possible to register an implementation statically by adding an entry
454
457
{spring-framework-api}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
455
458
For instance, `@EventListener` is meta-annotated with `@Reflective` since the underlying implementation invokes the annotated method using reflection.
456
459
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.
458
461
This can be tuned by specifying a custom `ReflectiveProcessor` implementation via the
0 commit comments