Add notes on constructor and factory method overloading

Closes gh-32091
This commit is contained in:
Juergen Hoeller 2024-04-30 15:45:46 +02:00
parent 26706f0039
commit fab3633c75
2 changed files with 26 additions and 5 deletions

View File

@ -234,6 +234,10 @@ For details about the mechanism for supplying arguments to the constructor (if r
and setting object instance properties after the object is constructed, see and setting object instance properties after the object is constructed, see
xref:core/beans/dependencies/factory-collaborators.adoc[Injecting Dependencies]. xref:core/beans/dependencies/factory-collaborators.adoc[Injecting Dependencies].
NOTE: In the case of constructor arguments, the container can select a corresponding
constructor among several overloaded constructors. That said, to avoid ambiguities,
it is recommended to keep your constructor signatures as straightforward as possible.
[[beans-factory-class-static-factory-method]] [[beans-factory-class-static-factory-method]]
=== Instantiation with a Static Factory Method === Instantiation with a Static Factory Method
@ -294,6 +298,24 @@ For details about the mechanism for supplying (optional) arguments to the factor
and setting object instance properties after the object is returned from the factory, and setting object instance properties after the object is returned from the factory,
see xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail]. see xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail].
NOTE: In the case of factory method arguments, the container can select a corresponding
method among several overloaded methods of the same name. That said, to avoid ambiguities,
it is recommended to keep your factory method signatures as straightforward as possible.
[TIP]
====
A typical problematic case with factory method overloading is Mockito with its many
overloads of the `mock` method. Choose the most specific variant of `mock` possible:
[source,xml,indent=0,subs="verbatim,quotes"]
----
<bean id="clientService" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg type="java.lang.Class" value="examples.ClientService"/>
<constructor-arg type="java.lang.String" value="clientService"/>
</bean>
----
====
[[beans-factory-class-instance-factory-method]] [[beans-factory-class-instance-factory-method]]
=== Instantiation by Using an Instance Factory Method === Instantiation by Using an Instance Factory Method
@ -416,8 +438,8 @@ Kotlin::
====== ======
This approach shows that the factory bean itself can be managed and configured through This approach shows that the factory bean itself can be managed and configured through
dependency injection (DI). See xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail] dependency injection (DI).
. See xref:core/beans/dependencies/factory-properties-detailed.adoc[Dependencies and Configuration in Detail].
NOTE: In Spring documentation, "factory bean" refers to a bean that is configured in the NOTE: In Spring documentation, "factory bean" refers to a bean that is configured in the
Spring container and that creates objects through an 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
going to return for the same bean name. going to return for the same bean name.

View File

@ -111,7 +111,8 @@ a mock service with Mockito:
[source,xml,indent=0,subs="verbatim,quotes"] [source,xml,indent=0,subs="verbatim,quotes"]
---- ----
<bean id="accountService" class="org.mockito.Mockito" factory-method="mock"> <bean id="accountService" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.example.AccountService"/> <constructor-arg type="java.lang.Class" value="org.example.AccountService"/>
<constructor-arg type="java.lang.String" value="accountService"/>
</bean> </bean>
---- ----