javanoob
javanoob

Reputation: 6410

BeanInstantiationException:Cannot convert type from java.lang.String to required type --- idref tag

I am new to spring and trying to understand the functionality of the 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 use ref instead of idref it is working fine.

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

What am I doing wrong here?

Upvotes: 1

Views: 1444

Answers (1)

skaffman
skaffman

Reputation: 403491

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.

Upvotes: 1

Related Questions