Skip to content

Commit 0d90335

Browse files
committed
Document that circular dependencies should be avoided in AOT mode
Closes gh-33786
1 parent fc7b8ae commit 0d90335

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: framework-docs/modules/ROOT/pages/core/aot.adoc

+14
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,20 @@ For instance, autowiring on fields and methods will be skipped as they are handl
352352

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

355+
[[aot.bestpractices.circular-dependencies]]
356+
=== Avoid Circular Dependencies
357+
358+
Certain use cases can result in circular dependencies between one or more beans. With the
359+
regular runtime, it may be possible to wire those circular dependencies via `@Autowired`
360+
on setter methods or fields. However, an AOT-optimized context will fail to start with
361+
explicit circular dependencies.
362+
363+
In an AOT-optimized application, you should therefore strive to avoid circular
364+
dependencies. If that is not possible, you can use `@Lazy` injection points or
365+
`ObjectProvider` to lazily access or retrieve the necessary collaborating beans. See
366+
xref:core/beans/classpath-scanning.adoc#beans-factorybeans-annotations-lazy-injection-points[this tip]
367+
for further information.
368+
355369
[[aot.bestpractices.factory-bean]]
356370
=== FactoryBean
357371

Diff for: framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc

+5-1
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,15 @@ factory method and other bean definition properties, such as a qualifier value t
480480
the `@Qualifier` annotation. Other method-level annotations that can be specified are
481481
`@Scope`, `@Lazy`, and custom qualifier annotations.
482482

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

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

0 commit comments

Comments
 (0)