From 4ac521607eaf6ae143288f839cb0b1c981efe55e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 29 Feb 2024 17:51:12 +0100 Subject: [PATCH] Reference documentation for @Fallback See gh-26241 --- .../annotation-config/autowired-primary.adoc | 49 +++++++++++++++++-- .../autowired-qualifiers.adoc | 13 ++--- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-primary.adoc b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-primary.adoc index dcaa7bce6234..21a95fccc361 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-primary.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-primary.adoc @@ -1,5 +1,5 @@ [[beans-autowired-annotation-primary]] -= Fine-tuning Annotation-based Autowiring with `@Primary` += Fine-tuning Annotation-based Autowiring with `@Primary` or `@Fallback` Because autowiring by type may lead to multiple candidates, it is often necessary to have more control over the selection process. One way to accomplish this is with Spring's @@ -50,8 +50,51 @@ Kotlin:: ---- ====== -With the preceding configuration, the following `MovieRecommender` is autowired with the -`firstMovieCatalog`: +Alternatively, as of 6.2, there is a `@Fallback` annotation for demarcating +any beans other than the regular ones to be injected. If only one regular +bean is left, it is effectively primary as well: + +[tabs] +====== +Java:: ++ +[source,java,indent=0,subs="verbatim,quotes",role="primary"] +---- + @Configuration + public class MovieConfiguration { + + @Bean + public MovieCatalog firstMovieCatalog() { ... } + + @Bean + @Fallback + public MovieCatalog secondMovieCatalog() { ... } + + // ... + } +---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] +---- + @Configuration + class MovieConfiguration { + + @Bean + fun firstMovieCatalog(): MovieCatalog { ... } + + @Bean + @Fallback + fun secondMovieCatalog(): MovieCatalog { ... } + + // ... + } +---- +====== + +With both variants of the preceding configuration, the following +`MovieRecommender` is autowired with the `firstMovieCatalog`: [tabs] ====== diff --git a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc index ff9cad8976bf..77e78db6149c 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc @@ -1,12 +1,13 @@ [[beans-autowired-annotation-qualifiers]] = Fine-tuning Annotation-based Autowiring with Qualifiers -`@Primary` is an effective way to use autowiring by type with several instances when one -primary candidate can be determined. When you need more control over the selection process, -you can use Spring's `@Qualifier` annotation. You can associate qualifier values -with specific arguments, narrowing the set of type matches so that a specific bean is -chosen for each argument. In the simplest case, this can be a plain descriptive value, as -shown in the following example: +`@Primary` and `@Fallback` are effective ways to use autowiring by type with several +instances when one primary (or non-fallback) candidate can be determined. + +When you need more control over the selection process, you can use Spring's `@Qualifier` +annotation. You can associate qualifier values with specific arguments, narrowing the set +of type matches so that a specific bean is chosen for each argument. In the simplest case, +this can be a plain descriptive value, as shown in the following example: -- [tabs]