spring xml configuration

Spring idref element example and usage

Spring idref is used to pass the id of a bean which is a string value (not a reference) of another bean in the Spring XML configuration nested inside <property/> or <constructor-arg/> element. In other words it is used when you need to pass the id of a bean to another bean as a String value in a property or constructor arguments. In this tutorial let’s see spring idref example and its usage.

Spring idref usage

Let’s see how to use idref element in Spring XML configuration.

<bean id="dept1" class="com.sneppets.idref.Department">
	
    <property name="departmentName" value="Sales" />
		
</bean>

<bean id="employee1" class="com.sneppets.idref.Employee">
	
    <property name="empName" value="John"/>
		
    <property name="dept">
        <idref bean="dept1"/>
    </property>
		
</bean>

The above bean definition is exactly equivalent to the following configuration

<bean id="dept1" class="com.sneppets.idref.Department">
	
    <property name="departmentName" value="Sales" />
		
</bean>

<bean id="employee1" class="com.sneppets.idref.Employee">
	
    <property name="empName" value="John"/>
		
    <property name="dept" value ="dept1"/>
		
</bean>

The first form which uses idref element is preferred over the other, because when you use idref tag in XML configuration it lets the container to perform validation to check whether the referenced named bean actually exists or not during deployment time.

When you use the second one, no validation will be performed on the value that you are passing to the dept property of the employee1 bean. Therefore any issues will be identified only when the employee1 bean is actually instantiated.

Where it is used commonly ?

The <idref/> element is commonly used in AOP interceptors in a ProxyFactoryBean  bean definitions.

Spring idref Example

Now, let’s see complete example using idref element.

Spring XML configuration (idref-example.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-4.3.xsd">

	<bean id="dept1" class="com.sneppets.idref.Department">
	
		<property name="departmentName" value="Sales" />
		
	</bean>
	
	<bean id="dept2" class="com.sneppets.idref.Department">
	
		<property name="departmentName" value="Marketting" />
		
	</bean>
		
	<bean id="employee1" class="com.sneppets.idref.Employee">
	
		<property name="empName" value="John"/>
		
		<property name="dept">
			<idref bean="dept1"/>
		</property>
		
	</bean>
	
	<bean id="employee2" class="com.sneppets.idref.Employee">
	
		<property name="empName" value="Paul"/>
		
		<property name="dept">
			<idref bean="dept2"/>
		</property>
		
	</bean>	
	

</beans>

Department.java

package com.sneppets.idref;

public class Department {
	
	private String departmentName;

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

}

Employee.java

package com.sneppets.idref;

public class Employee {
	
	private String empName;
	
	private String dept;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

}

SpringIdrefExample.java

package com.sneppets.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sneppets.idref.Department;
import com.sneppets.idref.Employee;

public class SpringIdrefExample {
	
	public static void main(String[] args) {

    	ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext(
    			"idref-example.xml");
    	Employee emp1 = (Employee) context.getBean("employee1");
    	String idrefBean1 = emp1.getDept();  
    	
    	System.out.println("Employee Name : " + emp1.getEmpName());
    	System.out.println("idref bean name : " + idrefBean1);
    	
    	Department dept1 = (Department) context.getBean(idrefBean1);
    	System.out.println("Department Name : " + dept1.getDepartmentName());
    	
    	Employee emp2 = (Employee) context.getBean("employee2");
    	String idrefBean2 = emp2.getDept();
    	
    	System.out.println("Employee Name : " + emp2.getEmpName());
    	System.out.println("idref bean name : " + idrefBean2);
    	
    	Department dept2 = (Department) context.getBean(idrefBean2);
    	System.out.println("Department Name : " + dept2.getDepartmentName());
    	
    	
	}

}

Output

Employee Name : John
idref bean name : dept1
Department Name : Sales

Employee Name : Paul
idref bean name : dept2
Department Name : Marketting

You also should know that, the local attribute on idref element is no longer supported in 4.0 beans XSD, so you need to change all existing idref local references to idref bean when you upgrade schema from old version to 4.0 version.

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments