Inject collection values by constructor

Inject collection values by constructor

We can inject collection values by constructor in spring framework. There can be used three elements inside the constructor-arg element.

It can be:

  • list
  • set
  • map

Each collection can have string based and non-string based values.Consider the example where one Person has multiple Hobbies.

In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Students.java file.

Students.java

This class contains three properties, constructor and display() method that prints the information. Here, we are using List to contain the multiple answers.


package com.codeNuclear;

import java.util.Iterator;
import java.util.List;

public class Students 
{
	private int id;
	private String name;
	private String question;
	private List<String> hobbies;
	
	public Students(int id,String name,String question,List<String> hobbies)
	{
		super();
		this.id = id;
		this.name = name;
		this.question = question;
		this.hobbies = hobbies;
	}
	
	public void display()
	{
		System.out.println("ID:"+id+" "+"Name:"+name+"\n"+"Question:"+question);
		System.out.println("\nHobbies are:");
		
		Iterator<String> itr = hobbies.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}
applicationContext.xml

The list element of constructor-arg is used here to define the list.


<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<bean id="q" class="com.codeNuclear.Students">  
<constructor-arg value="10"></constructor-arg>
<constructor-arg value="John"></constructor-arg>  
<constructor-arg value="What are your hobbies?"></constructor-arg>  
<constructor-arg>  
<list>  
<value>Watching and playing Cricket</value>  
<value>Reading Novel Book</value>  
<value>Volunteering and Community Participation</value>  
</list>  
</constructor-arg>  
</bean>  
  
</beans>  
Test.java

This class gets the bean from the applicationContext.xml file and calls the display() method.


package com.codeNuclear;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

	public static void main(String[] args) 
	{
		
		Resource r=new ClassPathResource("applicationContext.xml");  
	    BeanFactory factory=new XmlBeanFactory(r);  
	      
	    Students q=(Students)factory.getBean("q");  
	    q.display();
	}

}

Output ID:10 Name:John Question:What are your hobbies? Hobbies are:Watching and playing Cricket,Reading Novel Book,Volunteering and Community Participation