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

Customer.java

This class contains three properties with setters and getters and show() method that shows the information. Here, we are using List to display the list of customer names.

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 :

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment