Home » Spring » Constructor Injection With Collection Of Custom Object

Constructor Injection With Collection Of Custom Object

We can inject collection of custom object by using the ref element inside the list, set or map tag in spring configuration file.

Let us see an example of injecting collection of custom objects by using constructor injection.In this example, department class has list of employee object.

Files Required :

  1. Department.java
  2. Employee.java
  3. bean.xml
  4. Test.java

Creating Bean Class

Create two bean class Department.java and Employee.java inside the package com.jwt.spring.bean.

Department.java

package com.jwt.spring;
import java.util.List;
public class Department {
	private int id;
	private String departmentName;
	List<Employee> empList;

	public Department(int id, String departmentName, List<Employee> empList) {
		this.id = id;
		this.departmentName = departmentName;
		this.empList = empList;
	}
	public void displayResult() {
		System.out.println("Department ID is :" + id
				+ " and Department Name is " + departmentName);
		System.out.println("Employees are:");
		for (Employee employee : empList) {
			System.out.println(employee);
		}
	}
}

Employee.java

package com.jwt.spring;
public class Employee {
	private int id;
	private String name;
	private String city;
	public Employee(int id, String name, String city) {
		this.id = id;
		this.name = name;
		this.city = city;
	}
	public String toString() {
		return "Employee ID :" + id + " Employee Name: " + name
				+ " Employee City: " + city;
	}
}

In the above class there are three attributes viz. id, name and city. All the three attributes are set through constructor injection. The toString() method of the class is used to display the Employee information.

Configure bean in configuration xml file

Below is the code snippet to use the Set in the spring bean configuration file.

bean.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-3.0.xsd">
	<bean id="emp1" class="com.jwt.spring.Employee">
		<constructor-arg value="100"></constructor-arg>
		<constructor-arg value="Mukesh"></constructor-arg>
		<constructor-arg value="Chennai"></constructor-arg>
	</bean>
	<bean id="emp2" class="com.jwt.spring.Employee">
		<constructor-arg value="101"></constructor-arg>
		<constructor-arg value="Ravi"></constructor-arg>
		<constructor-arg value="Delhi"></constructor-arg>
	</bean>
	<bean id="department" class="com.jwt.spring.Department">
		<constructor-arg value="100"></constructor-arg>
		<constructor-arg value="Finance"></constructor-arg>
		<constructor-arg>
			<list>
				<ref bean="emp1" />
				<ref bean="emp2" />
			</list>
		</constructor-arg>
	</bean>
</beans>

Creating Test Class

Create a class Test.java to test the program.

Test.java

package com.jwt.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"bean.xml");
		Department department = (Department) context.getBean("department");
		department.displayResult();
	}
}
Output:
Department ID is :100 and Department Name is Finance
Employees are:
Employee ID :100 Employee Name: Mukesh Employee City: Chennai
Employee ID :101 Employee Name: Ravi Employee City: Delhi

Note:

Similarly you can use Set instead of List for that only you need to change list to set in the bean.xml file and List to Set in the Department.java file.


Previous Next Article
comments powered by Disqus