Can someone tell me the difference between
<bean id="b1" class="" />
<bean id="" class="">
<property name="b1" ref="b1" />
</bean>
and
<bean id="" class="">
<property name="b1" idref="b1" />
</bean>
and which one has to be used when?
Spring ref attribute The ref attribute is a shortcut to the <ref> tag, used to refer to other beans for injection.
If you are referring to a bean in different XML file, you can reference it with a ' ref ' tag, ' bean ' attribute. <ref bean="someBean"/> In this example, the bean “OutputHelper” declared in ' Spring-Common. xml ' can access to other beans in ' Spring-Output.
The advantage of using idref over value attribute is that if there will be no bean with that id, it will throw error at runtime. The idref allows validating the existence of bean with that id at deployment time. The idref is used with <constructor-arg/> or <property/> element.
idref is used to pass the name (identifier) of a bean (that is, a String). <idref bean="pointA"> is exactly the same as just the string value pointA , except that Spring will complain if such a bean is not defined.
Here is a little more verbose example, suppose you have two beans A and B:
<bean class="A" id="a" />
<bean class="B"/>
<constructor-arg>
<ref bean="a"/>
<idref bean="a"/>
</constructor-arg>
</bean>
In this case B would have a constructor that would look like this:
public B(A a, String string) {
string.equals("a"); //true
}
So with ref you can reference an object and with idref you just reference the name of the bean
ref
is used to pass the bean that the ref refers to.idref
is used to pass the name of the bean (as aString
) that is referred to.
http://forum.springsource.org/showthread.php?t=74355
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With