Skip to content

Commit 42c17eb

Browse files
committed
Documentation notes for @bean overriding and bean name matching
See gh-31052 See gh-28122
1 parent b9a6ba1 commit 42c17eb

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,19 @@ Letting qualifier values select against target bean names, within the type-match
154154
candidates, does not require a `@Qualifier` annotation at the injection point.
155155
If there is no other resolution indicator (such as a qualifier or a primary marker),
156156
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.
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).
159159
160160
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.
161165
====
162166

163-
That said, if you intend to express annotation-driven injection by name, do not
164-
primarily use `@Autowired`, even if it is capable of selecting by bean name among
165-
type-matching candidates. Instead, use the JSR-250 `@Resource` annotation, which is
166-
semantically defined to identify a specific target component by its unique name, with
167-
the declared type being irrelevant for the matching process. `@Autowired` has rather
167+
As an alternative for injection by name, consider the JSR-250 `@Resource` annotation
168+
which is semantically defined to identify a specific target component by its unique name,
169+
with the declared type being irrelevant for the matching process. `@Autowired` has rather
168170
different semantics: After selecting candidate beans by type, the specified `String`
169171
qualifier value is considered within those type-selected candidates only (for example,
170172
matching an `account` qualifier against beans marked with the same qualifier label).

framework-docs/modules/ROOT/pages/core/beans/definition.adoc

+16-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ runtime (concurrently with live access to the factory) is not officially support
7373
lead to concurrent access exceptions, inconsistent state in the bean container, or both.
7474
====
7575

76+
77+
7678
[[beans-definition-overriding]]
7779
== Overriding Beans
7880

@@ -81,15 +83,26 @@ already allocated. While bean overriding is possible, it makes the configuration
8183
to read and this feature will be deprecated in a future release.
8284

8385
To disable bean overriding altogether, you can set the `allowBeanDefinitionOverriding`
84-
to `false` on the `ApplicationContext` before it is refreshed. In such setup, an
86+
flag to `false` on the `ApplicationContext` before it is refreshed. In such setup, an
8587
exception is thrown if bean overriding is used.
8688

8789
By default, the container logs every bean overriding at `INFO` level so that you can
8890
adapt your configuration accordingly. While not recommended, you can silence those logs
8991
by setting the `allowBeanDefinitionOverriding` flag to `true`.
9092

91-
NOTE: We acknowledge that overriding beans in a test is convenient, and there is
92-
explicit support for this. For more details please refer to xref:testing/testcontext-framework/bean-overriding.adoc[this section].
93+
.Java-configuration
94+
****
95+
If you use Java Configuration, a corresponding `@Bean` method always silently overrides
96+
a scanned bean class with the same component name as long as the return type of the
97+
`@Bean` method matches that bean class. This simply means that the container will call
98+
the `@Bean` factory method in favor of any pre-declared constructor on the bean class.
99+
****
100+
101+
NOTE: We acknowledge that overriding beans in a test scenario is convenient,
102+
and there is explicit support for this as of Spring Framework 6.2. Please refer to
103+
xref:testing/testcontext-framework/bean-overriding.adoc[this section] for more details.
104+
105+
93106

94107
[[beans-beanname]]
95108
== Naming Beans

0 commit comments

Comments
 (0)