W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/13/2017

Setter Injection with Map parameter in spring

Setter Injection with Map parameter in spring example.
In this example, we are using map as the answer for a question that have answer as the key and username as the value. Here, we are using key and value pair both as a string.
Like previous examples, it is the example of forum where one question can have multiple answers.
Question.java
This class contains three properties, getters & setters and displayInfo() method to display the information.
  1. package com.tpyyes;  
  2. import java.util.Iterator;  
  3. import java.util.Map;  
  4. import java.util.Set;  
  5. import java.util.Map.Entry;  
  6.   
  7. public class Question {  
  8. private int id;  
  9. private String name;  
  10. private Map<String,String> answers;  
  11.   
  12. //getters and setters  
  13.   
  14. public void displayInfo(){  
  15.     System.out.println("question id:"+id);  
  16.     System.out.println("question name:"+name);  
  17.     System.out.println("Answers....");  
  18.     Set<Entry<String, String>> set=answers.entrySet();  
  19.     Iterator<Entry<String, String>> itr=set.iterator();  
  20.     while(itr.hasNext()){  
  21.         Entry<String,String> entry=itr.next();  
  22.         System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());  
  23.     }  
  24. }  
  25. }  

applicationContext.xml
The entry attribute of map is used to define the key and value information.
  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="que" class="com.tpyyes.Question">  
  10. <property name="id" value="1"></property>  
  11. <property name="name" value="What is Java?"></property>  
  12. <property name="answers">  
  13. <map>  
  14. <entry key="Java is a programming language"  value="Sonoo Jaiswal"></entry>  
  15. <entry key="Java is a Platform" value="Sachin Yadav"></entry>  
  16. </map>  
  17. </property>  
  18. </bean>  
  19.   
  20. </beans>  
Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo() method.
  1. package com.tpyyes;  
  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 res=new ClassPathResource("applicationContext.xml");  
  11.     BeanFactory factory=new XmlBeanFactory(res);  
  12.       
  13.     Question q=(Question)factory.getBean("que");  
  14.     q.displayInfo();  
  15.       
  16. }  

No comments:

Post a Comment

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