Spring IDREF usage Spring IDREF usage spring spring

Spring IDREF usage


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.

See the Spring documentation for details.

To pass the actual bean just use ref, exactly as you do for pointB and pointC.


The idref element is simply an error-proof way to pass the id (string value - not a reference) of another bean in the container to a or element.

In simple the idref element is used to pass a string value and using the idref tag allows the container to validate at deployment time that the referenced, named bean actually exists.

consider the below example

class FirstBean

class SecondBean

Bean definition in the application context

the calling code for instantiating the beans

output in the console

Notice the output in the console when we invoke secondBean.getSecondMessage() the value is firstBean which was set using the idref attribute.

note: A common place where the element brings value is in the configuration of AOP interceptors in a ProxyFactoryBean bean definition. Using elements when you specify the interceptor names prevents you from misspelling an interceptor id.


By using ‘idref’ tag, you can validate at deployment time that the referenced, named bean actually exists or not.

For Example,

<bean id="paulo" class="com.sample.pojo.Author">    <property name="firstName" value="Paulo" />    <property name="lastName" value="Coelho" />    <property name="dateOfBirth" value="24 August 1947" />    <property name="country" value="India" /></bean>

If you define your validator bean like below, Spring validates the beans with ids osho and Paulo at deployment time. If any bean not found in the configuration file, then spring throws BeanDefinitionStoreException.

<bean id="validatorBean" class="com.sample.test.BeansValidator">    <property name="author1">        <idref bean="osho" />    </property>    <property name="author2">        <idref bean="paulo" />    </property></bean>

Following is the complete working application.

package com.sample.pojo;public class Author {    private String firstName;    private String lastName;    private String dateOfBirth;    private String country;    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName) {        this.firstName = firstName;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }    public String getDateOfBirth() {        return dateOfBirth;    }    public void setDateOfBirth(String dateOfBirth) {        this.dateOfBirth = dateOfBirth;    }    public String getCountry() {        return country;    }    public void setCountry(String country) {        this.country = country;    }    @Override    public String toString() {        StringBuilder builder = new StringBuilder();        builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)                .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");        return builder.toString();    }}

BeansValidator.java

package com.sample.test;    public class BeansValidator {        private String author1;        private String author2;        public String getAuthor1() {            return author1;        }        public void setAuthor1(String author1) {            this.author1 = author1;        }        public String getAuthor2() {            return author2;        }        public void setAuthor2(String author2) {            this.author2 = author2;        }    }

myConfiguration.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="osho" class="com.sample.pojo.Author">        <property name="firstName" value="Osho" />        <property name="lastName" value="Jain" />        <property name="dateOfBirth" value="11 December 1931" />        <property name="country" value="India" />    </bean>    <bean id="paulo" class="com.sample.pojo.Author">        <property name="firstName" value="Paulo" />        <property name="lastName" value="Coelho" />        <property name="dateOfBirth" value="24 August 1947" />        <property name="country" value="India" />    </bean>    <bean id="validatorBean" class="com.sample.test.BeansValidator">        <property name="author1">            <idref bean="osho" />        </property>        <property name="author2">            <idref bean="paulo" />        </property>    </bean></beans>

Run HelloWorld.java, you don’t get any exceptions.

package com.sample.test;    import org.springframework.context.ApplicationContext;    import org.springframework.context.support.ClassPathXmlApplicationContext;    public class HelloWorld {        public static void main(String args[]) {            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });            ((ClassPathXmlApplicationContext) context).close();        }    }

Now update myConfiguration.xml like below.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="osho" class="com.sample.pojo.Author">        <property name="firstName" value="Osho" />        <property name="lastName" value="Jain" />        <property name="dateOfBirth" value="11 December 1931" />        <property name="country" value="India" />    </bean>    <bean id="paulo" class="com.sample.pojo.Author">        <property name="firstName" value="Paulo" />        <property name="lastName" value="Coelho" />        <property name="dateOfBirth" value="24 August 1947" />        <property name="country" value="India" />    </bean>    <bean id="validatorBean" class="com.sample.test.BeansValidator">        <property name="author1">            <idref bean="Krishna" />        </property>        <property name="author2">            <idref bean="paulo" />        </property>    </bean></beans>

As you see the configuration file, validatorBean check for the bean with id ‘Krishna’.

<bean id="validatorBean" class="com.sample.test.BeansValidator">        <property name="author1">            <idref bean="Krishna" />        </property>        <property name="author2">            <idref bean="paulo" />        </property>    </bean>

Since the bean with id ‘Krishna’ don’t exists, you will end up in following error.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validatorBean' defined in class path resource [myConfiguration.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'Krishna' in bean reference for bean property 'author1'    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)    at com.sample.test.HelloWorld.main(HelloWorld.java:8)