W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/12/2017

Constructor Injection with Map in spring

Constructor Injection with Map in spring
In this example, we are using map as the answer that have answer with posted username. 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, two constructors and displayInfo() method to display the information.
  1. package com.javatpoint;  
  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. public Question() {}  
  13. public Question(int id, String name, Map<String, String> answers) {  
  14.     super();  
  15.     this.id = id;  
  16.     this.name = name;  
  17.     this.answers = answers;  
  18. }  
  19.   
  20. public void displayInfo(){  
  21.     System.out.println("question id:"+id);  
  22.     System.out.println("question name:"+name);  
  23.     System.out.println("Answers....");  
  24.     Set<Entry<String, String>> set=answers.entrySet();  
  25.     Iterator<Entry<String, String>> itr=set.iterator();  
  26.     while(itr.hasNext()){  
  27.         Entry<String,String> entry=itr.next();  
  28.         System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());  
  29.     }  
  30. }  
  31. }  
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.javatpoint.Question">  
  10. <constructor-arg value="11"></constructor-arg>  
  11. <constructor-arg value="What is Java?"></constructor-arg>  
  12. <constructor-arg>  
  13. <map>  
  14. <entry key="Java is a Programming Language"  value="Ajay Kumar"></entry>  
  15. <entry key="Java is a Platform" value="John Smith"></entry>  
  16. <entry key="Java is an Island" value="Raj Kumar"></entry>  
  17. </map>  
  18. </constructor-arg>  
  19. </bean>  
  20.   
  21. </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.