Skip to content

Commit 4ac5216

Browse files
committed
Reference documentation for @fallback
See gh-26241
1 parent 57c10a1 commit 4ac5216

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

Diff for: framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-primary.adoc

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[[beans-autowired-annotation-primary]]
2-
= Fine-tuning Annotation-based Autowiring with `@Primary`
2+
= Fine-tuning Annotation-based Autowiring with `@Primary` or `@Fallback`
33

44
Because autowiring by type may lead to multiple candidates, it is often necessary to have
55
more control over the selection process. One way to accomplish this is with Spring's
@@ -50,8 +50,51 @@ Kotlin::
5050
----
5151
======
5252

53-
With the preceding configuration, the following `MovieRecommender` is autowired with the
54-
`firstMovieCatalog`:
53+
Alternatively, as of 6.2, there is a `@Fallback` annotation for demarcating
54+
any beans other than the regular ones to be injected. If only one regular
55+
bean is left, it is effectively primary as well:
56+
57+
[tabs]
58+
======
59+
Java::
60+
+
61+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
62+
----
63+
@Configuration
64+
public class MovieConfiguration {
65+
66+
@Bean
67+
public MovieCatalog firstMovieCatalog() { ... }
68+
69+
@Bean
70+
@Fallback
71+
public MovieCatalog secondMovieCatalog() { ... }
72+
73+
// ...
74+
}
75+
----
76+
77+
Kotlin::
78+
+
79+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
80+
----
81+
@Configuration
82+
class MovieConfiguration {
83+
84+
@Bean
85+
fun firstMovieCatalog(): MovieCatalog { ... }
86+
87+
@Bean
88+
@Fallback
89+
fun secondMovieCatalog(): MovieCatalog { ... }
90+
91+
// ...
92+
}
93+
----
94+
======
95+
96+
With both variants of the preceding configuration, the following
97+
`MovieRecommender` is autowired with the `firstMovieCatalog`:
5598

5699
[tabs]
57100
======

Diff for: framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired-qualifiers.adoc

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[[beans-autowired-annotation-qualifiers]]
22
= Fine-tuning Annotation-based Autowiring with Qualifiers
33

4-
`@Primary` is an effective way to use autowiring by type with several instances when one
5-
primary candidate can be determined. When you need more control over the selection process,
6-
you can use Spring's `@Qualifier` annotation. You can associate qualifier values
7-
with specific arguments, narrowing the set of type matches so that a specific bean is
8-
chosen for each argument. In the simplest case, this can be a plain descriptive value, as
9-
shown in the following example:
4+
`@Primary` and `@Fallback` are effective ways to use autowiring by type with several
5+
instances when one primary (or non-fallback) candidate can be determined.
6+
7+
When you need more control over the selection process, you can use Spring's `@Qualifier`
8+
annotation. You can associate qualifier values with specific arguments, narrowing the set
9+
of type matches so that a specific bean is chosen for each argument. In the simplest case,
10+
this can be a plain descriptive value, as shown in the following example:
1011

1112
--
1213
[tabs]

0 commit comments

Comments
 (0)