English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

コンストラクタ注入(関連オブジェクトを持つ)の例

この例では、以下を使用します map AnswerとUserを持つ答えとして。ここでは、キーと値のペアをオブジェクトとして使用します。答えにはanswerId、答え、投稿日時などの独自の情報があり、ユーザーにはuserId、ユーザー名、emailIdなどの独自の情報があります。

前の例と同様に、これはフォーラムの例であり 一つの質問には複数の答えができます

Question.java

このクラスには3つの属性、2つのコンストラクタ、および情報を表示するdisplayInfo()メソッドがあります。

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("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.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 toString(){
    return "ID:"+ID+" Answer:"+answer+" Posted Date:"+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 toString(){
    return "ID:"+ID+"名前:"+名前+"メール ID:"+メール;
}
}

applicationContext.xml

エントリ 要素 キー-ref および 値-ref 属性は、地図内でbeanの参照を定義するために使用されます。

<?xml version="1.0"エンコーディング="UTF-8"-8"?>
<beans
    xmlns="http://www.springframework.org/スキーマ/beans
    xmlns:xsi="http://www.w3.org/2001/XMLスキーマ-インスタンス
    xmlns:p="http://www.springframework.org/スキーマ/p
    xsi:schemaLocation="http://www.springframework.org/スキーマ/beans 
http://www.springframework.org/スキーマ/beans/spring-beans-3.0.xsd">
<bean id="アンサー1" class="com.w3codebox.Answer">
<コンストラクタ-arg value="""1"></コンストラクタ-arg>
<コンストラクタ-arg value="Javaはプログラミング言語"></コンストラクタ-arg>
<コンストラクタ-arg value=""12/12/2001></コンストラクタ-arg>
</bean>
<bean id="アンサー2" class="com.w3codebox.Answer">
<コンストラクタ-arg value="""2"></コンストラクタ-arg>
<コンストラクタ-arg value="Javaはプラットフォーム"></コンストラクタ-arg>
<コンストラクタ-arg value=""12/12/2003></コンストラクタ-arg>
</bean>
<bean id="ユーザー1" class="com.w3codebox.User">
<コンストラクタ-arg value=""1></コンストラクタ-arg>
<コンストラクタ-arg value="Arun Kumar"></コンストラクタ-arg>
<コンストラクタ-arg value="[email protected]"></コンストラクタ-arg>
</bean>
<bean id="ユーザー2" class="com.w3codebox.User">
<コンストラクタ-arg value=""2></コンストラクタ-arg>
<コンストラクタ-arg value="Varun Kumar"></コンストラクタ-arg>
<コンストラクタ-arg value="[email protected]"></コンストラクタ-arg>
</bean>
<bean id="q" class="com.w3codebox.Question">
<コンストラクタ-arg value=""1></コンストラクタ-arg>
<コンストラクタ-arg value="Javaとは何ですか?"></コンストラクタ-arg>
<コンストラクタ-arg>
<マップ>
<エントリ キー-ref="アンサー1"値-ref="ユーザー1></エントリ>
<エントリ キー-ref="アンサー2"値-ref="ユーザー2></エントリ>
</マップ>
</コンストラクタ-arg>
</bean>
</beans>

Test.java

このクラスはapplicationContext.xmlファイルからBeanを取得し、displayInfo()メソッドを呼び出して情報を表示します。

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();
}
}