Skip to content

Commit

Permalink
Document that circular dependencies should be avoided in AOT mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 7, 2024
1 parent fc7b8ae commit 0d90335
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions framework-docs/modules/ROOT/pages/core/aot.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,20 @@ For instance, autowiring on fields and methods will be skipped as they are handl

Rather than having prototype-scoped beans created with custom arguments, we recommend a manual factory pattern where a bean is responsible for the creation of the instance.

[[aot.bestpractices.circular-dependencies]]
=== Avoid Circular Dependencies

Certain use cases can result in circular dependencies between one or more beans. With the
regular runtime, it may be possible to wire those circular dependencies via `@Autowired`
on setter methods or fields. However, an AOT-optimized context will fail to start with
explicit circular dependencies.

In an AOT-optimized application, you should therefore strive to avoid circular
dependencies. If that is not possible, you can use `@Lazy` injection points or
`ObjectProvider` to lazily access or retrieve the necessary collaborating beans. See
xref:core/beans/classpath-scanning.adoc#beans-factorybeans-annotations-lazy-injection-points[this tip]
for further information.

[[aot.bestpractices.factory-bean]]
=== FactoryBean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,15 @@ factory method and other bean definition properties, such as a qualifier value t
the `@Qualifier` annotation. Other method-level annotations that can be specified are
`@Scope`, `@Lazy`, and custom qualifier annotations.

TIP: In addition to its role for component initialization, you can also place the `@Lazy`
[[beans-factorybeans-annotations-lazy-injection-points]]
[TIP]
====
In addition to its role for component initialization, you can also place the `@Lazy`
annotation on injection points marked with `@Autowired` or `@Inject`. In this context,
it leads to the injection of a lazy-resolution proxy. However, such a proxy approach
is rather limited. For sophisticated lazy interactions, in particular in combination
with optional dependencies, we recommend `ObjectProvider<MyTargetBean>` instead.
====

Autowired fields and methods are supported, as previously discussed, with additional
support for autowiring of `@Bean` methods. The following example shows how to do so:
Expand Down

0 comments on commit 0d90335

Please sign in to comment.