tags:

views:

21

answers:

1

Hi All,

i am new to spring and trying to understand the functionality of idref tag

My config file is pasted here:

<bean id="AudioSystembean" class="com.springexample.AudioSystem">
<property name="price" value="200"/>
</bean>

<bean id="Vehicle1" class="com.SpringExample.Vehicle1">
<property name="vehicleType" value="CAR" />
<property name="audioSystem" >
<idref bean = "AudioSystembean" /> 
</property>
</bean>

when i am executing the code i am getting the error as : "BeanInstantiationException:Cannot convert type from java.lang.String to required type :com.SpringExampl.AudioSystem" [I dont remember the exact exception - I have the code at home]

If i i use ref instead of idref it is working fine.

I tried google to understand about idref but could not get much information..

whats wrong i am doing here?

+1  A: 

In a Spring context, <idref> is used to pass the name of the referenced bean, rather than the bean itself (which is what <ref> does). So in your example:

<property name="audioSystem" >
   <idref bean = "AudioSystembean" /> 
</property>

The audioSystem system property will be injected with the name of the AudioSystembean, i.e. the String "AudioSystembean".

This is not what you need here, though, you need the <ref> element, which passes a reference to the bean itself.

skaffman
Thanks skaffman for your reply and time. So you are saying that here ideref is passing just the string but not the bean which is required by the "audioSystem" property. The link you gave is useful as i got little understanding of idref..but not fully :( Is it acheivable to inject "audioSystem" property with "AudioSystem" type bean using idref tag.
javanoob
@javanerd: As I said, `<idref>` is not used for passing references, you need to use `<ref bean="..."/>` for that.
skaffman
@skaffman - thanks for the reply, i just read the reference and understood that it is used to pass only bean id(String) not the reference of the bean.But i have one doubt..Why would we want to retrieve the id of the bean? Any real time use of this?Thanks for ur time skaffman.
javanoob
@javanerd: I've never used it myself, but it can come in handy. The link I posted describes some use cases for it.
skaffman