在此示例中,我們使用
map 作為具有Answer和User的答案。在這里,我們將鍵和值對(duì)都用作對(duì)象。答案具有自己的信息,例如answerId,答案和postedDate,用戶(hù)具有自己的信息,例如userId,用戶(hù)名,emailId。
像前面的示例一樣,它是論壇的示例其中
一個(gè)問(wèn)題可以有多個(gè)答案。
Question.java
此類(lèi)包含三個(gè)屬性,兩個(gè)構(gòu)造函數(shù)和用于顯示信息的displayInfo()方法。
package com.nhooo;
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("question id:"+id);
System.out.println("question name:"+name);
System.out.println("Answers....");
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("Answer Information:");
System.out.println(ans);
System.out.println("Posted By:");
System.out.println(user);
}
}
}
Answer.java
package com.nhooo;
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 toString(){
return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate;
}
}
User.java
package com.nhooo;
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 toString(){
return "Id:"+id+" Name:"+name+" Email Id:"+email;
}
}
applicationContext.xml
條目
element 的
key-ref 和
value-ref 屬性用于在地圖中定義bean的引用。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="answer1" class="com.nhooo.Answer">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="Java is a Programming Language"></constructor-arg>
<constructor-arg value="12/12/2001"></constructor-arg>
</bean>
<bean id="answer2" class="com.nhooo.Answer">
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="Java is a Platform"></constructor-arg>
<constructor-arg value="12/12/2003"></constructor-arg>
</bean>
<bean id="user1" class="com.nhooo.User">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="Arun Kumar"></constructor-arg>
<constructor-arg value="arun@gmail.com"></constructor-arg>
</bean>
<bean id="user2" class="com.nhooo.User">
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="Varun Kumar"></constructor-arg>
<constructor-arg value="Varun@gmail.com"></constructor-arg>
</bean>
<bean id="q" class="com.nhooo.Question">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="What is Java?"></constructor-arg>
<constructor-arg>
<map>
<entry key-ref="answer1" value-ref="user1"></entry>
<entry key-ref="answer2" value-ref="user2"></entry>
</map>
</constructor-arg>
</bean>
</beans>
Test.java
此類(lèi)從applicationContext.xml文件獲取Bean,并調(diào)用displayInfo()方法以顯示信息。
package com.nhooo;
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();
}
}