Spring Setter Injection With Non-String Collection


We can inject the dependency in Setter based dependency injection by the use of a Non-string collection or array of objects. In some cases, we may need to inject an array of objects into another object. If we have a dependent object in the collection, we can perform this injection by using the ref element inside the list, set, or map.

Here in this example, we will use a list, set, or map element inside the property element. For your better understanding, we are using a simple example of a Forum where One question can have multiple answers. Here the answers are defined as the dependent objects that contain their information such as answerId, answer, and postedBy. This example is also called the non-string example for injecting the dependency in the setter injection of the Spring framework.

In other words, we are providing an example where a question has an array of answers and each answer has unique information.

Now for Spring setter injection, we need to create the following four classes;

  • Question.java
  • Answer.java
  • applicationContext.xml
  • Test.java


First of all, we need to create a class named question. It is the base class that can have duplicate answers or several answers. In this example, we are using lists that can have duplicate elements. You may use set elements that have unique elements. But, later you are required to change the list to set element when you are creating the applicationContext.xml file and List to Set in the creation of Question.java file.

Question.java class contains three properties (used as its three answers), two constructors, and the displayInfo() method that gives the output of the question. Here, the List element is used to store many answers.

Now we will create Qestion.java class with the help of the below code:

package com.intellinuts;  

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

public class Question {  
private int id;  
private String name;  
private List<Answer> answers;  

//setters and getters  

public void displayInfo(){  
    System.out.println(id+" "+name);  
    System.out.println("answers are:");  
    Iterator<Answer> itr=answers.iterator();  



After the creation of the Question class, we need to create an answer class that stores the values of id, name, and by as its unique information. This class also contains a constructor and a method named toString() which is called to display the answers. Here, each answer is dependent as it contains the unique values of its properties like id, name, and by.

To create an Answer class, just follow the code mentioned below:

package com.intellinuts;  

public class Answer {  
private int id;  
private String name;  
private String by;  

//setters and getters  

public String toString(){  
    return id+" "+name+" "+by;  


Now we will create an applicationContext.xml class which is used as the XML configuration file that contains all bean information. This class contains a ref element that is used to define the reference of another bean. Here, we are using the bean attribute of the ref element to specify the reference of another bean.

<?xml version="1.0" encoding="UTF-8"?>  

<bean id="answer1" class="com.intellinuts.Answer">  
<property name="id" value="1"></property>  
<property name="name" value="Java is a programming language"></property>  
<property name="by" value="Ravi Malik"></property>  
<bean id="answer2" class="com.intellinuts.Answer">  
<property name="id" value="2"></property>  
<property name="name" value="Java is a platform"></property>  
<property name="by" value="Sachin"></property>  


<bean id="q" class="com.intellinuts.Question">  
<property name="id" value="1"></property>  
<property name="name" value="What is Java?"></property>  
<property name="answers">  
<ref bean="answer1"/>  
<ref bean="answer2"/>  



Finally, we need to create a Test.java class to run the program we created. This class gets the bean from the applicationContext.xml file and calls the displayInfo() method.

package com.intellinuts;  

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

    Question q=(Question)factory.getBean("q");  


The output of the above program will be seen as bellow:

Question: What is Java?
id-> 1 name-> Java is a programming language by-> Ravi Malik
id-> 2 name-> Java is a platform by-> Sachin