English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In diesem Beispiel verwenden wir
map
als Antwort mit Answer und User. Hier verwenden wir sowohl Schlüssel als auch Wertpaare als Objekte. Die Antwort hat ihre eigenen Informationen, wie z.B. answerId, Antwort und postedDate, der Benutzer hat seine eigenen Informationen, wie z.B. userId, Benutzername, emailId.
Wie im obigen Beispiel ist es ein Beispiel für ein Forum, wo
Ein Problem kann mehrere Antworten haben
。
Question.java
Diese Klasse enthält drei Eigenschaften, zwei Konstruktoren und die Methode displayInfo() zur Anzeige von Informationen.
package com.w3codebox; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.Map.Entry; public class Question { private int id; private String name; private Map<Answer,User> answers; public Question() {} public Question(int id, String name, Map<Answer, User> answers) { super(); this.id = id; this.name = name; this.answers = answers; } public void displayInfo(){ System.out.println("Frage id:");+id); System.out.println("Frage name:");+name); System.out.println("Antworten...."); Set<Entry<Answer, User>> set = answers.entrySet(); Iterator<Entry<Answer, User>> itr = set.iterator(); while(itr.hasNext()){ Entry<Answer, User> entry = itr.next(); Answer ans = entry.getKey(); User user = entry.getValue(); System.out.println("Antwortinformation:"); System.out.println(ans); System.out.println("Gepostet von:"); System.out.println(user); } } }
Answer.java
package com.w3codebox; import java.util.Date; public class Answer { private int id; private String answer; private Date postedDate; public Answer() {} public Answer(int id, String answer, Date postedDate) { super(); this.id = id; this.answer = answer; this.postedDate = postedDate; } public String zuString(){ return "ID:"+ID+" Antwort:"+answer+" Gespeicherte Datum:"+postedDate; } }
User.java
package com.w3codebox; public class User { private int id; private String name, email; public User() {} public User(int id, String name, String email) { super(); this.id = id; this.name = name; this.email = email; } public String zuString(){ return "ID:"+ID+" Name:"+Name+" Email-ID:"+email; } }
applicationContext.xml
Eintrags
Elementes
des
Schlüssel-ref und
Wert-ref Diese Eigenschaft wird verwendet, um Referenzen von Beans in der Karte zu definieren.
<?xml version="1.0" encoding="UTF-8"?> <Beans xmlns="http://www.springframework.org/Schema/Beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instanz xmlns:p="http://www.springframework.org/Schema/p xsi:schemaLocation="http://www.springframework.org/Schema/Beans http://www.springframework.org/Schema/Beans/Spring-Beans-3.0.xsd"> <Bean id="antwort1" class="com.w3codebox.Antwort"> <Konstruktor-arg Wert="1"></Konstruktor-arg> <Konstruktor-arg Wert="Java ist eine Programmiersprache"></Konstruktor-arg> <Konstruktor-arg Wert="12/12/2001></Konstruktor-arg> </bean> <Bean id="antwort2" class="com.w3codebox.Antwort"> <Konstruktor-arg Wert="2"></Konstruktor-arg> <Konstruktor-arg Wert="Java ist eine Plattform"></Konstruktor-arg> <Konstruktor-arg Wert="12/12/2003></Konstruktor-arg> </bean> <Bean id="benutzer1" class="com.w3codebox.Benutzer"> <Konstruktor-arg Wert="1></Konstruktor-arg> <Konstruktor-arg Wert="Arun Kumar"></Konstruktor-arg> <Konstruktor-arg Wert="[email protected]"></Konstruktor-arg> </bean> <Bean id="benutzer2" class="com.w3codebox.Benutzer"> <Konstruktor-arg Wert="2></Konstruktor-arg> <Konstruktor-arg Wert="Varun Kumar"></Konstruktor-arg> <Konstruktor-arg Wert="[email protected]"></Konstruktor-arg> </bean> <Bean id="q" Klasse="com.w3codebox.Frage"> <Konstruktor-arg Wert="1></Konstruktor-arg> <Konstruktor-arg Wert="Was ist Java?"></Konstruktor-arg> <Konstruktor-arg> <Karte> <Eintrag Schlüssel-ref="antwort1" Wert-ref="user1></Eintrag> <Eintrag Schlüssel-ref="antwort2" Wert-ref="user2></Eintrag> </Karte> </Konstruktor-arg> </bean> </beans>
Test.java
Diese Klasse liest Beans aus der Datei applicationContext.xml und ruft die displayInfo()-Methode auf, um Informationen anzuzeigen.
package com.w3codebox; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Test { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); Question q=(Question)factory.getBean("q"); q.displayInfo(); } }