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

비 문자열 매핑 Setter 주입 예제

이 예제에서는 map Answer와 User를 가진 답변으로 사용됩니다. 여기서는 키와 값 모두를 객체로 사용합니다. 답변은 자신의 정보를 가지고 있으며 예를 들어 answerId, 답변, postedDate가 있으며, 사용자는 자신의 정보를 가지고 있으며 예를 들어 userId, 사용자 이름, emailId가 있습니다.

이전 예제와 같이, 이는 포럼 예제 중 하나입니다 하나의 질문에는 여러 가지 답변이 있을 수 있습니다

Question.java

이 클래스는 세 가지 속성을 포함하고 있으며 getters와 setters 메서드와 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;
//getters and setters
public void displayInfo(){
    System.out.println("question id:")+id);
    System.out.println("question name:")+이름);
    System.out.println("답변들...");
    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("답변 정보:");
        System.out.println(ans);
        System.out.println("게시자:");
        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 "아이디:"+아이디+"답변:"+답변+"게시일:"+게시일;
}
}

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 "아이디:"+아이디+"이름:"+이름+"이메일 아이디:"+email;
}
}

applicationContext.xml

항목 요소 key-ref value-ref 속성은 맵에서 베인 참조를 정의하는 데 사용됩니다。

<?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.w3codebox.Answer>
<property name="id" value="1></property>
<property name="answer" value="Java is a Programming Language"></property>
<property name="postedDate" value="12/12/2001></property>
</bean>
<bean id="answer2" class="com.w3codebox.Answer>
<property name="id" value="2></property>
<property name="answer" value="Java is a Platform"></property>
<property name="postedDate" value="12/12/2003></property>
</bean>
<bean id="user1" class="com.w3codebox.User>
<property name="id" value="1></property>
<property name="name" value="Arun Kumar"></property>
<property name="email" value="[email protected]"></property>
</bean>
<bean id="user2" class="com.w3codebox.User>
<property name="id" value="2></property>
<property name="name" value="Varun Kumar"></property>
<property name="email" value="[email protected]"></property>
</bean>
<bean id="q" class="com.w3codebox.Question>
<property name="id" value="1></property>
<property name="name" value="What is Java?"></property>
<property name="answers">
<map>
<entry key-ref="answer1" value-ref="user1></entry>
<entry key-ref="answer2" value-ref="user2></entry>
</map>
</property>
</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();
    
}
}