Constructor dependency injection with Non-String collection is used when you have a dependent object in the collection. This is done by using the ref element inside the list, set, or map.
In this useful guide, we are using the example of a Forum where a single person can have multiple answers. Here, the answer has its info such as answerId, answer, and postedBy. In this example, we are using four pages that are:
Here, we will use a ‘list’ that can have duplicate elements. Although, you may use ‘Set’ as it includes only the unique elements. Here, you will be required to convert the list to set in the applicationContext.xml file and List to Set in the Question.java file.
This class includes three properties, two constructors, and displayInfo() method to display the information. Here, the list will be used to contain multiple answers.
package com.intellinuts; import java.util.Iterator; import java.util.List; public class Question { private int id; private String name; private List<Answer> answers; public Question() {} public Question(int id, String name, List<Answer> answers) { super(); this.id = id; this.name = name; this.answers = answers; } 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()); } } }
This class includes three properties that are id, name, and by with constructor and toString() method.
package com.intellinuts; public class Answer { private int id; private String name; private String by; public Answer() {} public Answer(int id, String name, String by) { super(); this.id = id; this.name = name; this.by = by; } public String toString(){ return id+" "+name+" "+by; } }
On this page, a ‘ref’ element is used to define the reference of another bean. Here, the bean attribute of the ref element will be used 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="ans1" class="com.intellinuts.Answer"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="Java is a programming language"></constructor-arg> <constructor-arg value="John"></constructor-arg> </bean> <bean id="ans2" class="com.intellinuts.Answer"> <constructor-arg value="2"></constructor-arg> <constructor-arg value="Java is a Platform"></constructor-arg> <constructor-arg value="Ravi"></constructor-arg> </bean> <bean id="q" class="com.intellinuts.Question"> <constructor-arg value="111"></constructor-arg> <constructor-arg value="What is java?"></constructor-arg> <constructor-arg> <list> <ref bean="ans1"/> <ref bean="ans2"/> </list> </constructor-arg> </bean> </beans>
Here, this class will be used to retrieve the bean from the applicationContext.xml file that 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(); } }
Output:
Question> What is Java? Answer 1> Java is a programming language Name> John Answer 2> Java is a Platform Name> Ravi