Home  >  Spring

Spring idref Example

By Arvind Rai, May 07, 2020
On this page we will provide Spring idref example. Spring idref element is used to pass the id of a bean to another bean as string. The idref works same as value attribute but when we use idref then there must be a bean with that id which we pass to idref. 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. Now we will discuss using idref by example.
app-context.xml
<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="holyCityOfShiva" class="com.concretepage.City">
		<property name="cityName" value="Varanasi" />
		<property name="country" value="India" />
	</bean>
	<bean id="student" class="com.concretepage.Student">
	        <property name="name" value="Mahesh" />
		<property name="locationId">
			<idref bean="holyCityOfShiva" />
		</property>
		<property name="city" ref="holyCityOfShiva" />		
	</bean>
</beans> 
In the above XML, we are passing holyCityOfShiva as string to idref element. We can also observe that in our XML we have a bean with holyCityOfShiva id. If there will be no bean with this id, idref will throw error. When we print the value of locationId of Student class, it will be holyCityOfShiva as string.
The idref element and ref attribute are two different things. The idref only assigns the value as string whereas ref assigns a class as bean.
The idref works same as value attribute as given below.
Using value.
<property name="locationId" value="holyCityOfShiva" /> 
Using idref.
<property name="locationId">
   <idref bean="holyCityOfShiva" />
</property> 
Using idref ensures that there is bean with id holyCityOfShiva in our XML configuration. The idref is useful in the case when we want to pass a bean name to another bean as string.
The idref is used with <constructor-arg/> as following.
<bean id="student" class="com.concretepage.Student">
   <constructor-arg type="java.lang.String" value="Mahesh"/>
   <constructor-arg type="java.lang.String">
	   <idref bean="holyCityOfShiva" />
   </constructor-arg>
   <constructor-arg type="com.concretepage.City" ref="holyCityOfShiva"/>			
</bean> 

Now Find the other files used in our demo.
Student.java
package com.concretepage;
public class Student {
  private String name;
  private String locationId;
  private City city;
  public Student() {}
  public Student(String name, String locationId, City city) {
	this.name = name;
	this.locationId = locationId;
	this.city = city;
  }
  //Setters and Getters 
} 
City.java
package com.concretepage;
public class City {
  private String cityName;
  private String country;
  //Setters and Getters
} 
AppDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppDemo {
  public static void main(String... args) {
	AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");
	Student st = (Student) context.getBean("student");
	System.out.println("name: " + st.getName());
	System.out.println("locationId: " + st.getLocationId());
	System.out.println("city: " + st.getCity().getCityName());
	context.close();
  }
} 
Output
name: Mahesh
locationId: holyCityOfShiva
city: Varanasi 

Reference

Spring doc: idref

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us