Skip to content

Commit d4f4c69

Browse files
committed
Document @fallback alongside Primary in the reference docs and @bean Javadoc
Closes gh-36439 (cherry picked from commit 27686dc)
1 parent 74ab662 commit d4f4c69

6 files changed

Lines changed: 28 additions & 20 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ injected into a `Set<MovieCatalog>` annotated with `@Qualifier("action")`.
151151
[TIP]
152152
====
153153
Letting qualifier values select against target bean names, within the type-matching
154-
candidates, does not require a `@Qualifier` annotation at the injection point.
155-
If there is no other resolution indicator (such as a qualifier or a primary marker),
156-
for a non-unique dependency situation, Spring matches the injection point name
157-
(that is, the field name or parameter name) against the target bean names and chooses
158-
the same-named candidate, if any (either by bean name or by associated alias).
159-
160-
Since version 6.1, this requires the `-parameters` Java compiler flag to be present.
161-
As of 6.2, the container applies fast shortcut resolution for bean name matches,
162-
bypassing the full type matching algorithm when the parameter name matches the
163-
bean name and no type, qualifier or primary conditions override the match. It is
164-
therefore recommendable for your parameter names to match the target bean names.
154+
candidates, does not require a `@Qualifier` annotation at the injection point. If there
155+
is no other resolution indicator (such as a qualifier, a primary marker, or a fallback
156+
marker), for a non-unique dependency situation, Spring matches the injection point name
157+
(that is, the field name or parameter name) against the target bean names and chooses the
158+
same-named candidate, if any (either by bean name or by associated alias).
159+
160+
Since version 6.1, this requires the `-parameters` Java compiler flag to be present. As
161+
of 6.2, the container applies fast shortcut resolution for bean name matches, bypassing
162+
the full type matching algorithm when the parameter name matches the bean name and no
163+
type, qualifier, primary, or fallback conditions override the match. It is therefore
164+
recommendable for your parameter names to match the target bean names.
165165
====
166166

167167
As an alternative for injection by name, consider the JSR-250 `@Resource` annotation

framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ set of multiple matches for the specific bean type (as returned by the factory m
308308
309309
Note that the standard `jakarta.annotation.Priority` annotation is not available at the
310310
`@Bean` level, since it cannot be declared on methods. Its semantics can be modeled
311-
through `@Order` values in combination with `@Primary` on a single bean for each type.
311+
through `@Order` values in combination with `@Primary` or `@Fallback` on a single bean
312+
for each type.
312313
====
313314

314315
Even typed `Map` instances can be autowired as long as the expected key type is `String`.

framework-docs/modules/ROOT/pages/core/beans/annotation-config/custom-autowire-configurer.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ with the `CustomAutowireConfigurer`
2727

2828
When multiple beans qualify as autowire candidates, the determination of a "`primary`" is
2929
as follows: If exactly one bean definition among the candidates has a `primary`
30-
attribute set to `true`, it is selected.
30+
attribute set to `true`, it is selected. For annotation-based configuration, see
31+
xref:core/beans/annotation-config/autowired-primary.adoc[Fine-tuning with `@Primary` or `@Fallback`].

framework-docs/modules/ROOT/pages/web/webflux/config.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ Kotlin::
251251
======
252252

253253
TIP: If you need to have a `LocalValidatorFactoryBean` injected somewhere, create a bean and
254-
mark it with `@Primary` in order to avoid conflict with the one declared in the MVC config.
254+
mark it with `@Primary`, or mark the one declared in the MVC config with `@Fallback`, in
255+
order to avoid conflict.
255256

256257

257258
[[webflux-config-content-negotiation]]

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/validation.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ example shows:
1919
include-code::./MyController[tag=snippet,indent=0]
2020

2121
TIP: If you need to have a `LocalValidatorFactoryBean` injected somewhere, create a bean and
22-
mark it with `@Primary` in order to avoid conflict with the one declared in the MVC configuration.
22+
mark it with `@Primary`, or mark the one declared in the MVC configuration with
23+
`@Fallback`, in order to avoid conflict.

spring-context/src/main/java/org/springframework/context/annotation/Bean.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
* return obj;
5858
* }</pre>
5959
*
60-
* <h3>Profile, Scope, Lazy, DependsOn, Primary, Order</h3>
60+
* <h3>Profile, Scope, Lazy, DependsOn, Primary, Fallback, Order</h3>
6161
*
6262
* <p>Note that the {@code @Bean} annotation does not provide attributes for profile,
63-
* scope, lazy, depends-on or primary. Rather, it should be used in conjunction with
64-
* {@link Scope @Scope}, {@link Lazy @Lazy}, {@link DependsOn @DependsOn} and
63+
* scope, lazy, depends-on, or primary. Rather, it should be used in conjunction with
64+
* {@link Scope @Scope}, {@link Lazy @Lazy}, {@link DependsOn @DependsOn}, and
6565
* {@link Primary @Primary} annotations to declare those semantics. For example:
6666
*
6767
* <pre class="code">
@@ -82,6 +82,9 @@
8282
* through direct references, which is typically helpful for singleton startup.
8383
* {@code @Primary} is a mechanism to resolve ambiguity at the injection point level
8484
* if a single target component needs to be injected but several beans match by type.
85+
* {@code @Fallback} marks a bean as a fallback candidate in such scenarios; if all
86+
* beans but one among multiple matching candidates are marked as fallback, the
87+
* remaining bean will be selected.
8588
*
8689
* <p>Additionally, {@code @Bean} methods may also declare qualifier annotations
8790
* and {@link org.springframework.core.annotation.Order @Order} values, to be
@@ -97,8 +100,8 @@
97100
* orthogonal concern determined by dependency relationships and {@code @DependsOn}
98101
* declarations as mentioned above. Also, {@link jakarta.annotation.Priority} is not
99102
* available at this level since it cannot be declared on methods; its semantics can
100-
* be modeled through {@code @Order} values in combination with {@code @Primary} on
101-
* a single bean per type.
103+
* be modeled through {@code @Order} values in combination with {@code @Primary} or
104+
* {@code @Fallback} on a single bean per type.
102105
*
103106
* <h3>{@code @Bean} Methods in {@code @Configuration} Classes</h3>
104107
*
@@ -230,6 +233,7 @@
230233
* @see DependsOn
231234
* @see Lazy
232235
* @see Primary
236+
* @see Fallback
233237
* @see org.springframework.stereotype.Component
234238
* @see org.springframework.beans.factory.annotation.Autowired
235239
* @see org.springframework.beans.factory.annotation.Value

0 commit comments

Comments
 (0)