idref in Spring? What is the purpose of idref tag in Spring?

Today I took more time to understand the purpose of tag in Spring. I didn’t expect this tag creates more confusion among developers as I see in Google there were many post and discussions occurred against the tag.

Not only me, many struggled with the below spring error message seems 😀

“Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘square’ defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘java.lang.String’ to required type ‘com.saravanansivaji.spring.Point’ for property ‘pointD’; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.saravanansivaji.spring.Point] for property ‘pointD’: no matching editors or conversion strategy found”

It is a simple concept actually

When I got this error?

I have a class called ‘Square.java‘, with one attribute, “pointD”


package com.saravanansivaji.spring;

public class Square {
private Point pointD;

//Getters and Setters for pointD defined
}

package com.saravanansivaji.spring;

public class Point {
private int X;
private int Y;

public int getX() {
return X;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
}

The below is my spring configuration file

<bean id="square" class="com.saravanansivaji.spring.Square">
<property name="pointD">
<idref bean="squarePointD"/>
</property>
</bean>

<bean id="squarePointD" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="40"></property>
</bean>

The above gives the following error

Cannot convert value of type [java.lang.String] to required type [com.saravanansivaji.spring.Point] for property ‘pointD’: no matching editors or conversion strategy found

After some 1.5 hours of struggle, I got to understand that,

the below one,

<property name="pointD">
<idref bean="squarePointD"/>
</property>

is equals to the below one

<property name="pointD">
<value>squarePointD</value>
</property>

So why this error occurs?

The purpose of idref is, it is used to pass the name (identifier) of a bean to the calling class. I didn’t understand this and I see many didn’t understand this immediately.

In my example, Square is the calling class, with a attribute “pointD” of type com.saravanansivaji.spring.Point. But idref has the capability only to pass a bean name (string identifier – type “java.lang.String”) to the calling class. So here I am trying to assign the “squarePointD” of type java.lang.String as value to the attribute ‘pointD’ which is of type com.saravanansivaji.spring.Point.

The above code never works. So I introduced a new String property called “name” in the “Square” class. And the value to this property will be assigned by the attribute. So is that the only purpose you may have question? The ultimate purpose of idref tag is, the idref tag allows the container to validate at deployment time that the referenced, named bean actually exists.

Here is the final code snippets

package com.saravanansivaji.spring;

public class Square {
private Point pointD;
private String name;

//Getters and Setters
}

The below is my spring configuration file now with little changes

<bean id="square" class="com.saravanansivaji.spring.Square">
<property name="pointD">
<ref bean="squarePointD"/> <!-- only ref attribute works here, not the idref attribute -->
</property>

<property name="name"> <!-- assigning the name of a bean correctly to a matching property "name" of type 'java.lang.String' in Square class -->
<idref bean="squarePointD"/>
<!-- idref validates the existence of a bean with name "squarePointD".
If bean exist, it will pass the bean name as string value to the caller
If not exist, throws exception
-->
</property>
</bean>

<bean id="squarePointD" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="40"></property>
</bean>

Advertisements

4 Comments

  1. Sasha
    Posted August 11, 2015 at 9:22 am | Permalink | Reply

    Thank you VERY MUCH!! Finally, it’s clear! To use idref your class must have at least one instance variable of String type.

  2. ning
    Posted March 19, 2016 at 2:32 am | Permalink | Reply

    Thank you very much, your article helps me understand what “idref ” in spring framework means.

  3. Satya
    Posted April 24, 2016 at 1:33 am | Permalink | Reply

    Thank a lot

  4. satya
    Posted April 24, 2016 at 1:34 am | Permalink | Reply

    Thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: