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

Setter injection (example with related objects)

If there are dependent objects in the collection, you can use list , set in ref elements to inject this information. Or Map. Here, we will property elements use list, set, or map elements.

In this example, we take the forum as an example, where A question can have multiple answers. But Answer has its own information, such as answerId, answer, and postedBy. In this example, four pages are used:

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

In this example, the list we use can contain duplicate elements, and you can use a set that contains only unique elements. However, you need to change the list set in applicationContext.xml and the list set in Question.java.

Question.java

This class includes three properties, two constructors, and the displayInfo() method to display information. Here, we use a list to contain multiple answers.

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

This class has three properties id, name, and by constructor and toString() method.

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 element is used to define a reference to another bean. Here, we use ref element bean Attributes are used to specify a reference to another bean.

<?xml version="1.0" encoding="UTF-8"1.0" encoding="UTF-8"-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

This class retrieves a Bean from applicationContext.xml file and calls the displayInfo method.

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