Setter Injection with Set

We know that Set is a child interface of Collection. When we want to represent a group of individual object as a single entity where duplicates are not allowed and insertion order is not preserved, then we should go to the collection Set.

Let's see a program for better clarity of Setter Injection with Set.

Required Files:

1. Customer.java

2. springConfig.java

3. Test.java


Customer.java
package java8s;
import java.util.*;
public class Customer {
    private Set customerData;
    public void setCustomerData(Set customerData) {
        this.customerData = customerData;
    }
    public void show() {
        System.out.println(customerData);
    }
}

springConfig.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">
    <beans>
    <bean id = "id2" class = "java8s.Customer">
        <property name = "customerData">
            <set>
                <value> Akshay </value> 
                <value> Santosh </value> 
                <value> Tilan </value> 
                <value> Khirod </value> 
                <value> Manu </value> 
            </set> 
        </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 resource = new ClassPathResource("springConfig.xml");
        BeanFactory bf = new XmlBeanFactory(resource);
        Customer c = (Customer) bf.getBean("id2");
        c.show();
    }
}

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