Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Constructor Injection with Collection 2


If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map.

In this example, we are taking the example of Forum where One question can have multiple answers. But Answer has its own information such as answerId, answer and postedBy. There are four pages used in this example:

  1. Question.java
  2. Answer.java
  3. applicationContext.xml
  4. Test.java

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 Question.java file.

Question.java

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

  1. package com.Kreationnext;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. public class Question {
  5. private int id;
  6. private String name;
  7. private List<Answer> answers;
  8. public Question() {}
  9. public Question(int id, String name, List<Answer> answers) {
  10.     super();
  11.     this.id = id;
  12.     this.name = name;
  13.     this.answers = answers;
  14. }
  15. public void displayInfo(){
  16.     System.out.println(id+” “+name);
  17.     System.out.println(“answers are:”);
  18.     Iterator<Answer> itr=answers.iterator();
  19.     while(itr.hasNext()){
  20.         System.out.println(itr.next());
  21.     }
  22. }
  23. }

Answer.java

This class has three properties id, name and by with constructor and toString() method.

  1. package com.Kreationnext;
  2. public class Answer {
  3. private int id;
  4. private String name;
  5. private String by;
  6. public Answer() {}
  7. public Answer(int id, String name, String by) {
  8.     super();
  9.     this.id = id;
  10.     this.name = name;
  11.     this.by = by;
  12. }
  13. public String toString(){
  14.     return id+” “+name+” “+by;
  15. }
  16. }

applicationContext.xml

The ref element is used to define the reference of another bean. Here, we are using bean attribute of ref element to specify the reference of another bean.

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:p=“http://www.springframework.org/schema/p”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
  8. <bean id=“ans1” class=“com.Kreationnext.Answer”>
  9. <constructor-arg value=“1”></constructor-arg>
  10. <constructor-arg value=“Java is a programming language”></constructor-arg>
  11. <constructor-arg value=“John”></constructor-arg>
  12. </bean>
  13. <bean id=“ans2” class=“com.Kreationnext.Answer”>
  14. <constructor-arg value=“2”></constructor-arg>
  15. <constructor-arg value=“Java is a Platform”></constructor-arg>
  16. <constructor-arg value=“Ravi”></constructor-arg>
  17. </bean>
  18. <bean id=“q” class=“com.Kreationnext.Question”>
  19. <constructor-arg value=“111”></constructor-arg>
  20. <constructor-arg value=“What is java?”></constructor-arg>
  21. <constructor-arg>
  22. <list>
  23. <ref bean=“ans1”/>
  24. <ref bean=“ans2”/>
  25. </list>
  26. </constructor-arg>
  27. </bean>
  28. </beans>

Test.java

This class gets the bean from the applicationContext.xml file and calls the displayInfo method.

  1. package com.Kreationnext;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.core.io.Resource;
  6. public class Test {
  7. public static void main(String[] args) {
  8.     Resource r=new ClassPathResource(“applicationContext.xml”);
  9.     BeanFactory factory=new XmlBeanFactory(r);
  10.     Question q=(Question)factory.getBean(“q”);
  11.     q.displayInfo();
  12. }
  13. }

Comments

comments