W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/13/2017

Setter Injection with Object collection in spring framework

Setter Injection with Object collection in spring framework.
If we have dependent object in the collection, we can inject these information by using the ref element inside the listset or map. Here, we will use list, set or map element inside the property element.
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 name and 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.tpyyes;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4.   
  5. public class Question {  
  6. private int id;  
  7. private String name;  
  8. private List<Answer> answers;  
  9.   
  10. //setters and getters  
  11.   
  12. public void displayInfo(){  
  13.     System.out.println(id+" "+name);  
  14.     System.out.println("answers are:");  
  15.     Iterator<Answer> itr=answers.iterator();  
  16.     while(itr.hasNext()){  
  17.         System.out.println(itr.next());  
  18.     }  
  19. }  
  20.   
  21. }  

Answer.java
This class has three properties id, name and by with constructor and toString() method.
  1. package com.tpyyes;    
  2. public class Answer {  
  3. private int id;  
  4. private String name;  
  5. private String by;  
  6.   
  7. //setters and getters  
  8.   
  9. public String toString(){  
  10.     return id+" "+name+" "+by;  
  11. }  
  12. }  
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.   
  9. <bean id="answer1" class="com.tpyyes.Answer">  
  10. <property name="id" value="1"></property>  
  11. <property name="name" value="Java is a programming language"></property>  
  12. <property name="by" value="Ravi Malik"></property>  
  13. </bean>  
  14. <bean id="answer2" class="com.tpyyes.Answer">  
  15. <property name="id" value="2"></property>  
  16. <property name="name" value="Java is a platform"></property>  
  17. <property name="by" value="Sachin"></property>  
  18. </bean>  
  19.   
  20. <bean id="que" class="com.tpyyes.Question">  
  21. <property name="id" value="1"></property>  
  22. <property name="name" value="What is Java?"></property>  
  23. <property name="answers">  
  24. <list>  
  25. <ref bean="answer1"/>  
  26. <ref bean="answer2"/>  
  27. </list>  
  28. </property>  
  29. </bean>  
  30.   
  31. </beans>  
Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo method.
  1. package com.javatpoint;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.ClassPathResource;  
  6. import org.springframework.core.io.Resource;  
  7.   
  8. public class Test {  
  9. public static void main(String[] args) {  
  10.     Resource r=new ClassPathResource("applicationContext.xml");  
  11.     BeanFactory factory=new XmlBeanFactory(r);  
  12.       
  13.     Question q=(Question)factory.getBean("que");  
  14.     q.displayInfo();  
  15.       
  16. }  
  17. }  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.