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
This commit augment the AOT section with best practices we have
experienced while working on the AOT engine. In particular, it describes
how a FactoryBean with an unresolved generic should be handled.
Closesgh-30434
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/core/aot.adoc
+126-1Lines changed: 126 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@ Applying such optimizations early implies the following restrictions:
18
18
** `@Profile`, in particular profile-specific configuration needs to be chosen at build time.
19
19
** `Environment` properties that impact the presence of a bean (`@Conditional`) are only considered at build time.
20
20
* Bean definitions with instance suppliers (lambdas or method references) cannot be transformed ahead-of-time (see related https://github.com/spring-projects/spring-framework/issues/29555[spring-framework#29555] issue).
21
-
* The return type of methods annotated with `@Bean` should be the most specific type possible (typically the concrete class, not an interface) in order to support proper type inference without invoking the corresponding `@Bean` method at build time.
21
+
* Make sure that the bean type is as precise as possible.
22
+
23
+
TIP: See also the xref:core/aot.adoc#aot.bestpractices[] section.
22
24
23
25
When these restrictions are in place, it becomes possible to perform ahead-of-time processing at build time and generate additional assets.
24
26
A Spring AOT processed application typically generates:
@@ -193,6 +195,129 @@ There is a bean definition for `dataSourceConfiguration` and one for `dataSource
193
195
When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
194
196
This supplier invokes the `dataSource()` method on the `dataSourceConfiguration` bean.
195
197
198
+
[[aot.bestpractices]]
199
+
== Best Practices
200
+
201
+
The AOT engine is designed to handle as many use cases as possible, with no code change in applications.
202
+
However, keep in mind that some optimizations are made at build time based on a static definition of the beans.
203
+
204
+
This section lists the best practices that make sure your application is ready for AOT.
205
+
206
+
[[aot.bestpractices.bean-type]]
207
+
=== Expose The Most Precise Bean Type
208
+
209
+
While your application may interact with an interface that a bean implements, it is still very important to declare the most precise type.
210
+
The AOT engine performs additional checks on the bean type, such as detecting the presence of `@Autowired` members, or lifecycle callback methods.
211
+
212
+
For `@Configuration` classes, make sure that the return type of the factory `@Bean` method is as precise as possible.
If you are registering bean definitions programmatically, consider using `RootBeanBefinition` as it allows to specify a `ResolvableType` that handles generics.
258
+
259
+
[[aot.bestpractices.factory-bean]]
260
+
=== FactoryBean
261
+
262
+
`FactoryBean` should be used with care as it introduces an intermediate layer in terms of bean type resolution that may not be conceptually necessary.
263
+
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).
264
+
265
+
If your `FactoryBean` implementation does not resolve the object type (i.e. `T`), extra care is necessary.
0 commit comments