Skip to content

Spring:- What is the difference between ref attribute and ref tag?

I am pretty new to spring and have been learning from the official docs. I found that to inject a dependency using XML configuration, there are two ways to declare refs:-

<bean name="parentBean" class="com.example.ParentBean">
   <property name="dependency1" ref="com.example.Dependency1"/>
</bean>

Or, I can use the ref tag, like:-

    <bean name="parentBean" class="com.example.ParentBean">
       <property name="dependency1"> 
         <ref bean="com.example.Dependency1"/>
       </property> 
    </bean>

Is there any difference between the two ref declarations? If not, why does spring allow two different methods?

Thanks in advance.

Answer

According to comments in the XSD (see <xsd:complexType name="propertyType">):

ref attribute: A short-cut alternative to a nested <ref bean='...'/>.

Is there any difference between the two ref declarations?
No.

If not, why does spring allow two different methods?
Convenience.