spring xml configuration

idref vs ref element in Spring bean declaration with example

This tutorial explains you the usage of idref vs ref element in spring bean declaration with example. In the previous tutorial we have seen how to use idref element and where it is used commonly with example.

idref vs ref element usage

The idref element is used to pass the id, 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.

And ref is the element used to pass a bean that it is referring to. In other words it is used to pass reference of a bean to another bean (a collaborator) managed by spring container. The ref element is the final element inside a <property/> or <constructor-arg/> element.

Example: idref vs ref

The below example demonstrates the usage that’s explained above. The following is the sample bean declaration (XML-based configuration metadata) which uses idref and ref.

<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>

	<!-- idref example -->	
	<bean id="employee1" class="com.sneppets.idref.Employee">
	
		<property name="empName" value="John"/>
		
		<property name="deptName">
			<idref bean="dept1"/>
		</property>
		
	</bean>
	
	<!-- ref example --> 
        <bean id="employee2" class="com.sneppets.idref.Employee">
	
		<property name="empName" value="Paul"/>
	
		<property name="department">		
			<ref bean="dept1"/>		
		</property>
		
	</bean>	
	

</beans>

Employee.java

package com.sneppets.idref;

public class Employee {
	
	private String empName;
	
	private String deptName;
	
	private Department department;

	public String getEmpName() {
		return empName;
	}

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

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}



}

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;
	}

}

SpringIdrefRefExample.java

package com.sneppets.ref;

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

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

public class SpringIdrefRefExample {
	
	public static void main (String[] args) {
		
	ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext(
    			"idref-ref-example.xml");
    	Employee emp1 = (Employee) context.getBean("employee1");
    	String idrefBean1 = emp1.getDeptName();  
    	
    	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");
    	
    	System.out.println("Employee Name : " + emp2.getEmpName());
    	System.out.println("Department Name : " + emp2.getDepartment().getDepartmentName());
    	
    	
	}

}

Output

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

When you look at the following code, XML bean configuration and the above output, emp1.getDeptName() will return the id (a string value not a reference) of another bean i.e., dept1 (id or string value) managed by the container. Therefore we can say, idref element is used to pass an id of another bean.

Employee emp1 = (Employee) context.getBean("employee1");
String idrefBean1 = emp1.getDeptName(); 
System.out.println("idref bean name : " + idrefBean1);

Similarly, when you look at the following code, XML bean configuration and the above output, emp2.getDepartment() will return reference of bean i.e., dept1 which is used to get the department name.

Employee emp2 = (Employee) context.getBean("employee2");
System.out.println("Department Name : " + emp2.getDepartment().getDepartmentName());

Also see:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments