Skip to content

Commit f481a4b

Browse files
committed
Polish reference documentation
1 parent 60b5bbe commit f481a4b

File tree

5 files changed

+83
-84
lines changed

5 files changed

+83
-84
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,19 @@ therefore recommendable for your parameter names to match the target bean names.
167167
As an alternative for injection by name, consider the JSR-250 `@Resource` annotation
168168
which is semantically defined to identify a specific target component by its unique name,
169169
with the declared type being irrelevant for the matching process. `@Autowired` has rather
170-
different semantics: After selecting candidate beans by type, the specified `String`
170+
different semantics: after selecting candidate beans by type, the specified `String`
171171
qualifier value is considered within those type-selected candidates only (for example,
172172
matching an `account` qualifier against beans marked with the same qualifier label).
173173

174174
For beans that are themselves defined as a collection, `Map`, or array type, `@Resource`
175175
is a fine solution, referring to the specific collection or array bean by unique name.
176-
That said, as of 4.3, you can match collection, `Map`, and array types through Spring's
176+
That said, you can match collection, `Map`, and array types through Spring's
177177
`@Autowired` type matching algorithm as well, as long as the element type information
178178
is preserved in `@Bean` return type signatures or collection inheritance hierarchies.
179179
In this case, you can use qualifier values to select among same-typed collections,
180180
as outlined in the previous paragraph.
181181

182-
As of 4.3, `@Autowired` also considers self references for injection (that is, references
182+
`@Autowired` also considers self references for injection (that is, references
183183
back to the bean that is currently injected). Note that self injection is a fallback.
184184
Regular dependencies on other components always have precedence. In that sense, self
185185
references do not participate in regular candidate selection and are therefore in

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,29 @@ lead to concurrent access exceptions, inconsistent state in the bean container,
7878
[[beans-definition-overriding]]
7979
== Overriding Beans
8080

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.
81+
Bean overriding occurs when a bean is registered using an identifier that is already
82+
allocated. While bean overriding is possible, it makes the configuration harder to read.
83+
84+
WARNING: Bean overriding will be deprecated in a future release.
8485

8586
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+
flag to `false` on the `ApplicationContext` before it is refreshed. In such a setup, an
8788
exception is thrown if bean overriding is used.
8889

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`.
90+
By default, the container logs every attempt to override a bean at `INFO` level so that
91+
you can adapt your configuration accordingly. While not recommended, you can silence
92+
those logs by setting the `allowBeanDefinitionOverriding` flag to `true`.
9293

93-
.Java-configuration
94+
.Java Configuration
9495
****
9596
If you use Java Configuration, a corresponding `@Bean` method always silently overrides
9697
a scanned bean class with the same component name as long as the return type of the
9798
`@Bean` method matches that bean class. This simply means that the container will call
9899
the `@Bean` factory method in favor of any pre-declared constructor on the bean class.
99100
****
100101

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
102+
NOTE: We acknowledge that overriding beans in test scenarios is convenient, and there is
103+
explicit support for this as of Spring Framework 6.2. Please refer to
103104
xref:testing/testcontext-framework/bean-overriding.adoc[this section] for more details.
104105

105106

Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
[[spring-testing-annotation-beanoverriding-mockitobean]]
22
= `@MockitoBean` and `@MockitoSpyBean`
33

4-
`@MockitoBean` and `@MockitoSpyBean` are used on test class fields to override beans in
5-
the test's `ApplicationContext` with a Mockito mock or spy, respectively. In the latter
6-
case, the original bean definition is not replaced, but instead an early instance of the
7-
bean is captured and wrapped by the spy.
4+
`@MockitoBean` and `@MockitoSpyBean` are used on fields in test classes to override beans
5+
in the test's `ApplicationContext` with a Mockito mock or spy, respectively. In the
6+
latter case, the original bean definition is not replaced, but instead an early instance
7+
of the bean is captured and wrapped by the spy.
88

9-
By default, the annotated field's type is used to search for candidate definitions to
10-
override. If multiple candidates match, the usual `@Qualifier` can be provided to
11-
narrow the candidate to override. Alternatively, a candidate whose bean definition name
12-
matches the name of the field will match.
9+
By default, the annotated field's type is used to search for candidate bean definitions
10+
to override. If multiple candidates match, `@Qualifier` can be provided to narrow the
11+
candidate to override. Alternatively, a candidate whose bean definition name matches the
12+
name of the field will match.
1313

1414
To use a by-name override rather than a by-type override, specify the `name` attribute
1515
of the annotation.
1616

1717
[WARNING]
1818
====
19-
The qualifiers, including the name of the field are used to determine if a separate
20-
`ApplicationContext` needs to be created. If you are using this feature to mock or
21-
spy the same bean in several tests, make sure to name the field consistently to avoid
19+
Qualifiers, including the name of the field, are used to determine if a separate
20+
`ApplicationContext` needs to be created. If you are using this feature to mock or spy
21+
the same bean in several tests, make sure to name the field consistently to avoid
2222
creating unnecessary contexts.
2323
====
2424

2525
Each annotation also defines Mockito-specific attributes to fine-tune the mocking details.
2626

2727
The `@MockitoBean` annotation uses the `REPLACE_OR_CREATE_DEFINITION`
2828
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
29-
30-
If no definition matches, then a definition is created on-the-fly.
29+
If no existing bean definition matches, a new bean definition is created on the fly.
3130

3231
The `@MockitoSpyBean` annotation uses the `WRAP_BEAN`
3332
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy],
34-
and the original instance is wrapped in a Mockito spy.
35-
36-
It requires that exactly one candidate definition exists.
33+
and the original instance is wrapped in a Mockito spy. This strategy requires that
34+
exactly one candidate bean definition exists.
3735

3836
The following example shows how to use the default behavior of the `@MockitoBean` annotation:
3937

@@ -44,20 +42,20 @@ Java::
4442
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
4543
----
4644
class OverrideBeanTests {
47-
@MockitoBean // <1>
48-
private CustomService customService;
45+
@MockitoBean // <1>
46+
CustomService customService;
4947
5048
// test case body...
5149
}
5250
----
5351
<1> Replace the bean with type `CustomService` with a Mockito `mock`.
5452
======
5553

56-
In the example above, we are creating a mock for `CustomService`. If more that
57-
one bean with such type exist, the bean named `customService` is considered. Otherwise,
58-
the test will fail and you will need to provide a qualifier of some sort to identify which
59-
of the `CustomService` beans you want to override. If no such bean exists, a bean
60-
definition will be created with an auto-generated bean name.
54+
In the example above, we are creating a mock for `CustomService`. If more than one bean
55+
of that type exists, the bean named `customService` is considered. Otherwise, the test
56+
will fail, and you will need to provide a qualifier of some sort to identify which of the
57+
`CustomService` beans you want to override. If no such bean exists, a bean definition
58+
will be created with an auto-generated bean name.
6159

6260
The following example uses a by-name lookup, rather than a by-type lookup:
6361

@@ -68,14 +66,14 @@ Java::
6866
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
6967
----
7068
class OverrideBeanTests {
71-
@MockitoBean(name = "service") // <1>
72-
private CustomService customService;
69+
@MockitoBean(name = "service") // <1>
70+
CustomService customService;
7371
7472
// test case body...
7573
7674
}
7775
----
78-
<1> Replace the bean named `service` with a Mockito `mock`.
76+
<1> Replace the bean named `service` with a Mockito `mock`.
7977
======
8078

8179
If no bean definition named `service` exists, one is created.
@@ -89,19 +87,19 @@ Java::
8987
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9088
----
9189
class OverrideBeanTests {
92-
@MockitoSpyBean // <1>
93-
private CustomService customService;
90+
@MockitoSpyBean // <1>
91+
CustomService customService;
9492
9593
// test case body...
9694
}
9795
----
9896
<1> Wrap the bean with type `CustomService` with a Mockito `spy`.
9997
======
10098

101-
In the example above, we are wrapping the bean with type `CustomService`. If more that
102-
one bean with such type exist, the bean named `customService` is considered. Otherwise,
103-
the test will fail and you will need to provide a qualifier of some sort to identify which
104-
of the `CustomService` beans you want to spy.
99+
In the example above, we are wrapping the bean with type `CustomService`. If more than
100+
one bean of that type exists, the bean named `customService` is considered. Otherwise,
101+
the test will fail, and you will need to provide a qualifier of some sort to identify
102+
which of the `CustomService` beans you want to spy.
105103

106104
The following example uses a by-name lookup, rather than a by-type lookup:
107105

@@ -112,12 +110,12 @@ Java::
112110
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
113111
----
114112
class OverrideBeanTests {
115-
@MockitoSpyBean(name = "service") // <1>
116-
private CustomService customService;
113+
@MockitoSpyBean(name = "service") // <1>
114+
CustomService customService;
117115
118116
// test case body...
119117
120118
}
121119
----
122-
<1> Wrap the bean named `service` with a Mockito `spy`.
120+
<1> Wrap the bean named `service` with a Mockito `spy`.
123121
======

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
[[spring-testing-annotation-beanoverriding-testbean]]
22
= `@TestBean`
33

4-
`@TestBean` is used on a test class field to override a specific bean in the test's
5-
`ApplicationContext` with an instance provided by a conventionally named static factory
6-
method.
4+
`@TestBean` is used on a field in a test class to override a specific bean in the test's
5+
`ApplicationContext` with an instance provided by a factory method.
76

8-
The associated factory method name is derived from the annotated field's name, or bean
9-
name if specified. A `static` method with no argument that returns a type compatible
10-
with the type of the bean to override is expected. To make things more explicit, or if
11-
you'd rather use a different name, the annotation allows for a specific method name to
12-
be provided.
7+
The associated factory method name is derived from the annotated field's name, or the
8+
bean name if specified. The factory method must be `static`, accept no arguments, and
9+
have a return type compatible with the type of the bean to override. To make things more
10+
explicit, or if you'd rather use a different name, the annotation allows for a specific
11+
method name to be provided.
1312

14-
By default, the annotated field's type is used to search for candidate definitions to
15-
override. If multiple candidates match, the usual `@Qualifier` can be provided to
16-
narrow the candidate to override. Alternatively, a candidate whose bean definition name
17-
matches the name of the field will match.
13+
By default, the annotated field's type is used to search for candidate bean definitions
14+
to override. If multiple candidates match, `@Qualifier` can be provided to narrow the
15+
candidate to override. Alternatively, a candidate whose bean definition name matches the
16+
name of the field will match.
1817

1918
To use a by-name override rather than a by-type override, specify the `name` attribute
2019
of the annotation.
2120

2221
[WARNING]
2322
====
24-
The qualifiers, including the name of the field are used to determine if a separate
25-
`ApplicationContext` needs to be created. If you are using this feature to override
26-
the same bean in several tests, make sure to name the field consistently to avoid
27-
creating unnecessary contexts.
23+
Qualifiers, including the name of the field, are used to determine if a separate
24+
`ApplicationContext` needs to be created. If you are using this feature to override the
25+
same bean in several tests, make sure to name the field consistently to avoid creating
26+
unnecessary contexts.
2827
====
2928

3029
The following example shows how to use the default behavior of the `@TestBean` annotation:
@@ -36,25 +35,24 @@ Java::
3635
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
3736
----
3837
class OverrideBeanTests {
39-
@TestBean // <1>
40-
private CustomService customService;
38+
@TestBean // <1>
39+
CustomService customService;
4140
4241
// test case body...
4342
44-
private static CustomService customService() { // <2>
43+
static CustomService customService() { // <2>
4544
return new MyFakeCustomService();
4645
}
4746
}
4847
----
49-
<1> Mark a field for overriding of the bean with type `CustomService`.
48+
<1> Mark a field for overriding the bean with type `CustomService`.
5049
<2> The result of this static method will be used as the instance and injected into the field.
5150
======
5251

53-
In the example above, we are overriding the bean with type `CustomService`. If more that
54-
one bean with such type exist, the bean named `customService` is considered. Otherwise,
55-
the test will fail and you will need to provide a qualifier of some sort to identify which
56-
of the `CustomService` beans you want to override.
57-
52+
In the example above, we are overriding the bean with type `CustomService`. If more than
53+
one bean of that type exists, the bean named `customService` is considered. Otherwise,
54+
the test will fail, and you will need to provide a qualifier of some sort to identify
55+
which of the `CustomService` beans you want to override.
5856

5957
The following example uses a by-name lookup, rather than a by-type lookup:
6058

@@ -65,17 +63,18 @@ Java::
6563
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
6664
----
6765
class OverrideBeanTests {
68-
@TestBean(name = "service", methodName = "createCustomService") // <1>
69-
private CustomService customService;
66+
@TestBean(name = "service", methodName = "createCustomService") // <1>
67+
CustomService customService;
7068
7169
// test case body...
7270
73-
private static CustomService createCustomService() { // <2>
71+
static CustomService createCustomService() { // <2>
7472
return new MyFakeCustomService();
7573
}
7674
}
7775
----
78-
<1> Mark a field for overriding of the bean with name `service`.
76+
<1> Mark a field for overriding the bean with name `service`, and specify that the
77+
factory method is named `createCustomService`.
7978
<2> The result of this static method will be used as the instance and injected into the field.
8079
======
8180

spring-test/src/main/java/org/springframework/test/context/bean/override/OverrideMetadata.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ protected OverrideMetadata(Field field, ResolvableType beanType, @Nullable Strin
7474
}
7575

7676
/**
77-
* Parse the given {@code testClass} and provide the use of bean override.
77+
* Parse the given {@code testClass} and build the corresponding list of
78+
* bean {@code OverrideMetadata}.
7879
* @param testClass the class to parse
79-
* @return a list of bean overrides metadata
80+
* @return a list of {@code OverrideMetadata}
8081
*/
8182
public static List<OverrideMetadata> forTestClass(Class<?> testClass) {
82-
List<OverrideMetadata> all = new LinkedList<>();
83-
ReflectionUtils.doWithFields(testClass, field -> parseField(field, testClass, all));
84-
return all;
83+
List<OverrideMetadata> metadataList = new LinkedList<>();
84+
ReflectionUtils.doWithFields(testClass, field -> parseField(field, testClass, metadataList));
85+
return metadataList;
8586
}
8687

8788
private static void parseField(Field field, Class<?> testClass, List<OverrideMetadata> metadataList) {
@@ -182,9 +183,9 @@ public boolean equals(Object other) {
182183

183184
@Override
184185
public int hashCode() {
185-
int hash = Objects.hash(getClass().hashCode(), this.beanType.getType(), this.beanName, this.strategy);
186+
int hash = Objects.hash(getClass(), this.beanType.getType(), this.beanName, this.strategy);
186187
return (this.beanName != null ? hash : hash +
187-
Objects.hash(this.field.getName(), Arrays.hashCode(this.field.getAnnotations())));
188+
31 * Objects.hash(this.field.getName(), Arrays.hashCode(this.field.getAnnotations())));
188189
}
189190

190191
@Override

0 commit comments

Comments
 (0)