logo

Spring Setter Injection With Non-String Collection


Show

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

Question.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();  
    while(itr.hasNext()){  
        System.out.println(itr.next());  
    }  
}  

}  

Answer.Java

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

ApplicationContext.xml

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"?>  
<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="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>  
<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>  

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

</beans> 

Test.Java

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");  
    q.displayInfo();  

}  
}  

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

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