Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Constructor Injection with Map 2


In this example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object. Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.

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.Kreationnext;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import java.util.Map.Entry;
  6. public class Question {
  7. private int id;
  8. private String name;
  9. private Map<Answer,User> answers;
  10. public Question() {}
  11. public Question(int id, String name, Map<Answer, User> answers) {
  12.     super();
  13.     this.id = id;
  14.     this.name = name;
  15.     this.answers = answers;
  16. }
  17. public void displayInfo(){
  18.     System.out.println(“question id:”+id);
  19.     System.out.println(“question name:”+name);
  20.     System.out.println(“Answers….”);
  21.     Set<Entry<Answer, User>> set=answers.entrySet();
  22.     Iterator<Entry<Answer, User>> itr=set.iterator();
  23.     while(itr.hasNext()){
  24.         Entry<Answer, User> entry=itr.next();
  25.         Answer ans=entry.getKey();
  26.         User user=entry.getValue();
  27.         System.out.println(“Answer Information:”);
  28.         System.out.println(ans);
  29.         System.out.println(“Posted By:”);
  30.         System.out.println(user);
  31.     }
  32. }
  33. }

Answer.java

  1. package com.Kreationnext;
  2. import java.util.Date;
  3. public class Answer {
  4. private int id;
  5. private String answer;
  6. private Date postedDate;
  7. public Answer() {}
  8. public Answer(int id, String answer, Date postedDate) {
  9.     super();
  10.     this.id = id;
  11.     this.answer = answer;
  12.     this.postedDate = postedDate;
  13. }
  14. public String toString(){
  15.     return “Id:”+id+” Answer:”+answer+” Posted Date:”+postedDate;
  16. }
  17. }

User.java

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

applicationContext.xml

The key-ref and value-ref attributes of entry element is used to define the reference of bean in the map.

  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=“answer1” 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=“12/12/2001”></constructor-arg>
  12. </bean>
  13. <bean id=“answer2” 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=“12/12/2003”></constructor-arg>
  17. </bean>
  18. <bean id=“user1” class=“com.Kreationnext.User”>
  19. <constructor-arg value=“1”></constructor-arg>
  20. <constructor-arg value=“Arun Kumar”></constructor-arg>
  21. <constructor-arg value=“arun@gmail.com”></constructor-arg>
  22. </bean>
  23. <bean id=“user2” class=“com.Kreationnext.User”>
  24. <constructor-arg value=“2”></constructor-arg>
  25. <constructor-arg value=“Varun Kumar”></constructor-arg>
  26. <constructor-arg value=“Varun@gmail.com”></constructor-arg>
  27. </bean>
  28. <bean id=“q” class=“com.Kreationnext.Question”>
  29. <constructor-arg value=“1”></constructor-arg>
  30. <constructor-arg value=“What is Java?”></constructor-arg>
  31. <constructor-arg>
  32. <map>
  33. <entry key-ref=“answer1” value-ref=“user1”></entry>
  34. <entry key-ref=“answer2” value-ref=“user2”></entry>
  35. </map>
  36. </constructor-arg>
  37. </bean>
  38. </beans>

Test.java

This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information.

  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