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

Injection par constructeur (objet dépendant) exemple

Si le jeu contient des objets dépendants, vous pouvez utiliser list , set dans ref éléments pour injecter ces informations. Ou Map.

Dans cet exemple, nous prenons l'exemple de "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 ont été 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 set 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

Ce classe contient trois attributs, deux constructeurs et la méthode displayInfo() pour afficher les informations. Ici, nous utilisons une liste pour inclure 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;
public Question() {}
public Question(int id, String name, List<Answer> answers) {
    super();
    this.id = id;
    this.name = name;
    this.answers = answers;
}
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

Cette classe possède trois propriétés id, name et by, un constructeur et une méthode toString().

package com.w3codebox;
public class Answer {
private int id;
private String name;
private String by;
public Answer() {}
public Answer(int id, String name, String by) {
    super();
    this.id = id;
    this.name = name;
    this.by = by;
}
public String toString(){
    retour id+" "+nom+" "+par;
}
}

applicationContext.xml

ref élément est utilisé pour définir une référence à un autre bean. Ici, nous utilisons ref de l'élément élément Propriétés pour spécifier une référence à un autre bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schéma/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xmlns:p="http://www.springframework.org/schéma/p
    xsi:schemaLocation="http://www.springframework.org/schéma/beans 
http://www.springframework.org/schéma/beans/spring-beans-3.0.xsd">
<bean id="ans1" class="com.w3codebox.Answer">
<constructeur-arg value="1></constructeur-arg>
<constructeur-arg value="Java est un langage de programmation"></constructeur-arg>
<constructeur-arg value="John"></constructeur-arg>
</bean>
<bean id="ans2" class="com.w3codebox.Answer">
<constructeur-arg value="2></constructeur-arg>
<constructeur-arg value="Java est une Plateforme"></constructeur-arg>
<constructeur-arg value="Ravi"></constructeur-arg>
</bean>
<bean id="q" class="com.w3codebox.Question">
<constructeur-arg value="111></constructeur-arg>
<constructeur-arg value="What is java?"></constructeur-arg>
<constructeur-arg>
<liste>
<ref bean="ans1"/>
<ref bean="ans2"/>
</liste>
</constructeur-arg>
</bean>
</beans>

Test.java

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