Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.sequence;
public class SequenceGenerator {
public SequenceGenerator() {}
public SequenceGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
...
}
In this case, the second constructor will be matched and picked because Spring can find a bean
whose type is compatible with
PrefixGenerator
.
<beans ...>
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator"
autowire="constructor">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>
<bean id="datePrefixGenerator"
class="com.apress.springenterpriserecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd" />
</bean>
</beans>
However, multiple constructors in a class may cause ambiguity in constructor argument matching.
The situation may be further complicated if you ask Spring to determine a constructor for you. So, if you
use this auto-wiring mode, take great care to avoid ambiguity.
Auto-Wiring by Auto-Detection
The auto-wiring mode
autodetect
asks Spring to decide the auto-wiring mode between
byType
and
constructor
. If at least a default constructor with no argument is found for that bean,
byType
will be
chosen. Otherwise,
constructor
will be chosen. Because the
SequenceGenerator
class has a default
constructor defined,
byType
will be chosen. That means the prefix generator will be injected via the
setter method.
<beans ...>
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator"
autowire="autodetect">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>
Search WWH ::
Custom Search