Setter Injection with List

We know that in core java, List is the child interface of Collection. When we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order is preserved, then we need List. We can create an object of List like

List al=new ArrayList();

Let's see a program for better understanding the concept of Setter Injection with List:

Required Files:

1. WelcomeBean.java

2. spConfig.xml

3. Test.java


WelcomeBean.java

package java8s;
import java.util.List;
public class WelcomeBean {
    private List employeesData;

    public void setEmployeesData(List employeesData) {
        this.employeesData = employeesData;
    }

    public void show() {
        System.out.println(employeesData);
    }
}

spconfig.xml

<? 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 = "id1" class = "java8s.WelcomeBean">
    <property name = "employeesData">
        <list>
            <value> Arunima Bairiganjan </value> 
            <value> Yogamaya Patra </value> 
            <value> Sutapa Dash </value> 
            <value> Archana Prusti </value> 
        </list> 
    </property> 
    </bean> 
</beans>

Test.java

package java8s;
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 res = new ClassPathResource("spconfig.xml");
        BeanFactory bf = new XmlBeanFactory(res);
        Object ob = bf.getBean("id1");
        WelcomeBean wb = (WelcomeBean) ob;
        wb.show();
    }
}
Output:

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext