The bean name in the <ref> element’s bean attribute can be a reference to any bean in the IoC container, even if it’s not defined in the same XML configuration file. If you are referring to a bean in the same XML file, you should use the local attribute, as it is an XML ID reference. Your XML editor can help to validate whether a bean with that ID exists in the same XML file
(i.e., the reference integrity).
<bean id="sequenceGenerator" class="com.sequence.SequenceGenerator"> … <property name="prefixGenerator"> <ref local="datePrefixGenerator" /> </property> </bean>
There is also a convenient shortcut to specify a bean reference in the ref attribute of the
<bean id="sequenceGenerator" class="com.sequence.SequenceGenerator"> … <property> element. <bean id="sequenceGenerator" class="com.sequence.SequenceGenerator"> … <property name="prefixGenerator" ref="datePrefixGenerator" /> </bean>
But in this way, your XML editor will not be able to validate the reference integrity. Actually, it has the same effect as specifying the <ref> element’s bean attribute. Spring 2.x provides another convenient shortcut for you to specify bean references. It’s by using the p schema to specify bean references as attributes of the <bean> element. This can shorten the lines of XML configuration.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sequenceGenerator" class="com.sequence.SequenceGenerator" p:suffix="A" p:initial="1000000" p:prefixGenerator-ref="datePrefixGenerator" /> </beans>
To distinguish a bean reference from a simple property value, you have to add the -ref suffix to the property name.