Skip to content

Commit eca2b96

Browse files
committed
Documentation notes for @bean overriding and bean name matching
Closes gh-32825
1 parent e48f37d commit eca2b96

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,15 @@ Letting qualifier values select against target bean names, within the type-match
153153
candidates, does not require a `@Qualifier` annotation at the injection point.
154154
If there is no other resolution indicator (such as a qualifier or a primary marker),
155155
for a non-unique dependency situation, Spring matches the injection point name
156-
(that is, the field name or parameter name) against the target bean names and chooses the
157-
same-named candidate, if any.
156+
(that is, the field name or parameter name) against the target bean names and chooses
157+
the same-named candidate, if any (either by bean name or by associated alias).
158158
159159
Since version 6.1, this requires the `-parameters` Java compiler flag to be present.
160160
====
161161

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

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

+25
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ lead to concurrent access exceptions, inconsistent state in the bean container,
7575

7676

7777

78+
[[beans-definition-overriding]]
79+
== Overriding Beans
80+
81+
Bean overriding is happening when a bean is registered using an identifier that is
82+
already allocated. While bean overriding is possible, it makes the configuration harder
83+
to read and this feature will be deprecated in a future release.
84+
85+
To disable bean overriding altogether, you can set the `allowBeanDefinitionOverriding`
86+
flag to `false` on the `ApplicationContext` before it is refreshed. In such setup, an
87+
exception is thrown if bean overriding is used.
88+
89+
By default, the container logs every bean overriding at `INFO` level so that you can
90+
adapt your configuration accordingly. While not recommended, you can silence those logs
91+
by setting the `allowBeanDefinitionOverriding` flag to `true`.
92+
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+
102+
78103
[[beans-beanname]]
79104
== Naming Beans
80105

0 commit comments

Comments
 (0)