From 0d9033592b4baa1e5790eb2e0d2a8c007101f4b8 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 7 Nov 2024 12:23:39 +0100 Subject: [PATCH] Document that circular dependencies should be avoided in AOT mode Closes gh-33786 --- framework-docs/modules/ROOT/pages/core/aot.adoc | 14 ++++++++++++++ .../ROOT/pages/core/beans/classpath-scanning.adoc | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/core/aot.adoc b/framework-docs/modules/ROOT/pages/core/aot.adoc index cdd7cf71b4..2a53060d05 100644 --- a/framework-docs/modules/ROOT/pages/core/aot.adoc +++ b/framework-docs/modules/ROOT/pages/core/aot.adoc @@ -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 diff --git a/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc b/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc index 8c067fed4f..59d53dda15 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc @@ -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` 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: