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

Exemple d'injection Setter (objet lié)

Si la collection contient des objets dépendants, vous pouvez utiliser list , set dans ref éléments. Ou MapIci, nous allons insérer ces informations dans property Les éléments internes utilisent des éléments list, set ou map.

Dans cet exemple, nous prenons l'exemple d'un forum où Une question peut avoir plusieurs réponsesMais Answer a ses propres informations, par exemple answerId, answer et postedBy. Dans cet exemple, quatre pages sont utilisées :

Question.java Answer.java applicationContext.xml Test.java

Dans cet exemple, la liste que nous utilisons peut contenir des éléments répétés, vous pouvez utiliser un ensemble qui ne contient que des éléments uniques. Cependant, vous devez modifier la liste définie dans le fichier applicationContext.xml et la liste définie dans le fichier Question.java.

Question.java

Cette classe contient trois attributs, deux constructeurs et la méthode displayInfo() pour afficher les informations. Ici, nous utilisons une liste pour contenir plusieurs réponses.

package com.w3codebox;
import java.util.Iterator;
import java.util.List;
public class Question {
private int id;
private String name;
private List<Answer> answers;
//setters and getters
public void displayInfo() {
    System.out.println(id+" ""+name);
    System.out.println("answers are:");
    Iterator<Answer> itr = answers.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }
}
}

Answer.java

此类具有三个属性id,name和by构造函数和toString()方法。

package com.w3codebox;
public class Answer {
private int id;
private String name;
private String by;
//setters and getters
public String toString(){
    return id+" ""+name+" ""+by;
}
}

applicationContext.xml

ref 元素用于定义另一个bean的引用。在这里,我们使用 ref 元素的 bean 属性来指定另一个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.w3codebox.Answer">
<property name="id" value="1></property>
<property name="name" value="Java is a programming language"></property>
<property name="by" value="Ravi Malik"></property>
</bean>
<bean id="answer2" class="com.w3codebox.Answer">
<property name="id" value="2></property>
<property name="name" value="Java is a platform"></property>
<property name="by" value="Sachin"></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">
<list>
<ref bean="answer1"/>
<ref bean="answer2"/>
</list>
</property>
</bean>
</beans>

Test.java

Cette classe récupère le Bean à partir du fichier applicationContext.xml et appelle la méthode 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();
    
}
}