Specifying Bean References for Constructor Arguments

Bean references can also be applied to constructor injection. For example, you can add a constructor that accepts a PrefixGenerator object as an argument.

package com.sequence;
public class SequenceGenerator {
private PrefixGenerator prefixGenerator;
public SequenceGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
}

In the <constructor-arg> element, you can enclose a bean reference by <ref> just like in the <property> element.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">

<constructor-arg>
<ref local="datePrefixGenerator" />
</constructor-arg>
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>

The shortcut for specifying a bean reference also works for <constructor-arg>.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<constructor-arg ref="datePrefixGenerator" />
</bean>