Skip to content

Commit fab3633

Browse files
committed
Add notes on constructor and factory method overloading
Closes gh-32091
1 parent 26706f0 commit fab3633

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ For details about the mechanism for supplying arguments to the constructor (if r
234234
and setting object instance properties after the object is constructed, see
235235
xref:core/beans/dependencies/factory-collaborators.adoc[Injecting Dependencies].
236236

237+
NOTE: In the case of constructor arguments, the container can select a corresponding
238+
constructor among several overloaded constructors. That said, to avoid ambiguities,
239+
it is recommended to keep your constructor signatures as straightforward as possible.
240+
237241

238242
[[beans-factory-class-static-factory-method]]
239243
=== Instantiation with a Static Factory Method
@@ -294,6 +298,24 @@ For details about the mechanism for supplying (optional) arguments to the factor
294298
and setting object instance properties after the object is returned from the factory,
295299
see xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail].
296300

301+
NOTE: In the case of factory method arguments, the container can select a corresponding
302+
method among several overloaded methods of the same name. That said, to avoid ambiguities,
303+
it is recommended to keep your factory method signatures as straightforward as possible.
304+
305+
[TIP]
306+
====
307+
A typical problematic case with factory method overloading is Mockito with its many
308+
overloads of the `mock` method. Choose the most specific variant of `mock` possible:
309+
310+
[source,xml,indent=0,subs="verbatim,quotes"]
311+
----
312+
<bean id="clientService" class="org.mockito.Mockito" factory-method="mock">
313+
<constructor-arg type="java.lang.Class" value="examples.ClientService"/>
314+
<constructor-arg type="java.lang.String" value="clientService"/>
315+
</bean>
316+
----
317+
====
318+
297319

298320
[[beans-factory-class-instance-factory-method]]
299321
=== Instantiation by Using an Instance Factory Method
@@ -416,8 +438,8 @@ Kotlin::
416438
======
417439

418440
This approach shows that the factory bean itself can be managed and configured through
419-
dependency injection (DI). See xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail]
420-
.
441+
dependency injection (DI).
442+
See xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail].
421443

422444
NOTE: In Spring documentation, "factory bean" refers to a bean that is configured in the
423445
Spring container and that creates objects through an
@@ -444,5 +466,3 @@ cases into account and returns the type of object that a `BeanFactory.getBean` c
444466
going to return for the same bean name.
445467

446468

447-
448-

framework-docs/modules/ROOT/pages/testing/spring-mvc-test-framework/server-setup-options.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ a mock service with Mockito:
111111
[source,xml,indent=0,subs="verbatim,quotes"]
112112
----
113113
<bean id="accountService" class="org.mockito.Mockito" factory-method="mock">
114-
<constructor-arg value="org.example.AccountService"/>
114+
<constructor-arg type="java.lang.Class" value="org.example.AccountService"/>
115+
<constructor-arg type="java.lang.String" value="accountService"/>
115116
</bean>
116117
----
117118

0 commit comments

Comments
 (0)