Spring Set Element

The set element will create a java.util.Set that can be injected into a constructor or property. Using a Set is identical to using a List except a Set will only have unique values.

Following example demonstrates Spring Set Element:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>

</dependencies>

1. POJO

Create Address Class as following.

package com.kruders.bean;

import java.util.Set;

public class Employee {
	private String name;
	private Set<Address> set;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Address> getSet() {
		return set;
	}
	public void setSet(Set<Address> set) {
		this.set = set;
	}
}

Now create Employee Class as following

package com.kruders.bean;

import java.util.List;

public class Employee {
	private String name;
	private List<Address> lists;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Address> getLists() {
		return lists;
	}
	public void setLists(List<Address> lists) {
		this.lists = lists;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="set">
       	<set>
           <bean class="com.kruders.bean.Address">
				<property name="city" value="Delhi" />
				<property name="country" value="India" />
		   </bean>
		   <bean class="com.kruders.bean.Address">
				<property name="city" value="Banglore" />
				<property name="country" value="India" />
		   </bean>
        </set>
      </property>
   </bean>
</beans>

3. Run Program

Create Main.java class that displays the employee details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Address;
import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("\nPrinting Set");
		for(Address address : employee.getSet()) {
			System.out.print("City " + address.getCity());
			System.out.println(", Country " + address.getCountry());
		}
		
	}
}

When you run the above example you’ll get an output like:

Name Puneet
Printing Set
City Delhi, Country India
City Banglore, Country India

The folder structure of the example is shown below in Figure 37.1

Figure 37.1 Figure 37.1

You can download the source code of this example here.

,

No comments yet.

Leave a Reply