Spring supports the following collections.

  1. List
  2. Set
  3. Map
  4. Properties

List Collection Example:

  1. Create the Employee class.
  2. package com.java2learn.core;
    
    public class Employee {
    
    	private String name;
    	private int age;
    	private double salary;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public double getSalary() {
    		return salary;
    	}
    
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    
    	@Override
    	public String toString() {
    		return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
    				+ "]";
    	}
    }
    
  3. Create the ListCollection class to inject List Collection.
  4. package com.java2learn.core;
    
    import java.util.List;
    
    public class ListCollection {
    	
    	List<Object> list;
    
    	public List<Object> getList() {
    		return list;
    	}
    
    	public void setList(List<Object> list) {
    		this.list = list;
    	}
    }
    
    
  5. Configure Employee and ListCollection classes into Spring-Core.xml file.
  6. <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-3.0.xsd">
    
    	<bean id="listCollection" class="com.java2learn.core.ListCollection">
    		<property name="list">
    			<list>
    				<value>1000</value>
    				<bean class="com.java2learn.core.Employee">
    					<property name="age" value="100"></property>
    					<property name="name" value="JAVA"></property>
    					<property name="salary" value="4522.22"></property>
    				</bean>
    				<ref local="emp"/>
    			</list>
    		</property>
    	</bean>
    	<bean id="emp" class="com.java2learn.core.Employee">
    		<property name="age" value="100"></property>
    		<property name="name" value="JAVA"></property>
    		<property name="salary" value="4522.22"></property>
    	</bean>
    </beans>
    
  7. Create the client and run it.
  8. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.java2learn.core.ListCollection;
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
    		
    		ListCollection list=(ListCollection)applicationContext.getBean("listCollection");
    		System.out.println(list.getList());
    		
    	}
    }
    
    

Output:

[1000, Employee [name=JAVA, age=100, salary=4522.22], Employee [name=JAVA, age=100, salary=4522.22]]

SetCollection Example:

  1. Create the Employee class.
  2. package com.java2learn.core;
    
    public class Employee {
    
    	private String name;
    	private int age;
    	private double salary;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public double getSalary() {
    		return salary;
    	}
    
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    
    	@Override
    	public String toString() {
    		return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
    				+ "]";
    	}
    
    }
    
    
  3. Create the SetCollection class to inject Set Collection.
  4. package com.java2learn.core;
    
    import java.util.Set;
    
    public class SetCollection {
    	
    	Set<Object> set;
    
    	public Set<Object> getSet() {
    		return set;
    	}
    
    	public void setSet(Set<Object> set) {
    		this.set = set;
    	}
    }
    
  5. Configure Employee and SetCollection classes into Spring-Core.xml file.
  6. <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-3.0.xsd">
    
    	<bean id="setCollection" class="com.java2learn.core.SetCollection">
    		<property name="set">
    			<set>
    				<value>1000</value>
    				<bean class="com.java2learn.core.Employee">
    					<property name="age" value="100"></property>
    					<property name="name" value="JAVA"></property>
    					<property name="salary" value="4522.22"></property>
    				</bean>
    				<ref local="emp"/>
    			</set>
    		</property>
    	</bean>
    	<bean id="emp" class="com.java2learn.core.Employee">
    		<property name="age" value="100"></property>
    		<property name="name" value="JAVA"></property>
    		<property name="salary" value="4522.22"></property>
    	</bean>
    </beans>
    
  7. Create the client and run it.
  8. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.java2learn.core.SetCollection;
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
    		
    		SetCollection list=(SetCollection)applicationContext.getBean("setCollection");
    		System.out.println(list.getSet());
    		
    	}
    }
    

Output:

[1000, Employee [name=JAVA, age=100, salary=4522.22], Employee [name=JAVA, age=100, salary=4522.22]]

MapCollection Example:

  1. Create the Employee class.
  2. package com.java2learn.core;
    
    public class Employee {
    
    	private String name;
    	private int age;
    	private double salary;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public double getSalary() {
    		return salary;
    	}
    
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    
    	@Override
    	public String toString() {
    		return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
    				+ "]";
    	}
    }
    
  3. Create the MapCollection class to inject Map Collection.
  4. package com.java2learn.core;
    
    import java.util.Map;
    
    public class MapCollection {
    	
    	Map<Object,Object> map;
    
    	public Map<Object, Object> getMap() {
    		return map;
    	}
    
    	public void setMap(Map<Object, Object> map) {
    		this.map = map;
    	}
    
    }
    
  5. Configure Employee and MapCollection classes into Spring-Core.xml file.
  6. <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-3.0.xsd">
    
    	<bean id="mapCollection" class="com.java2learn.core.MapCollection">
    		<property name="map">
    			<map>
    				<entry key="01" value="5000"></entry>
    				<entry key="02">
    					<bean class="com.java2learn.core.Employee">
    						<property name="age" value="52"></property>
    						<property name="name" value="JAVA"></property>
    					</bean>
    				</entry>
    				<entry key="03" value-ref="emp"></entry>
    			</map>
    		</property>
    	</bean>
    	<bean id="emp" class="com.java2learn.core.Employee">
    		<property name="age" value="100"></property>
    		<property name="name" value="JAVA"></property>
    		<property name="salary" value="4522.22"></property>
    	</bean>
    </beans>
    
  7. Create the client and run it.
  8. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.java2learn.core.MapCollection;
    
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-Core.xml");
    		MapCollection map = (MapCollection) applicationContext.getBean("mapCollection");
    		System.out.println(map.getMap());
    
    	}
    }
    

Output:

{01=5000, 02=Employee [name=JAVA, age=52, salary=0.0], 03=Employee [name=JAVA, age=100, salary=4522.22]}

Properties Example:

  1. Create PropertiesDemo class
  2. package com.java2learn.core;
    
    import java.util.Properties;
    
    public class PropertiesDemo {
    	Properties props;
    
    	public Properties getProps() {
    		return props;
    	}
    
    	public void setProps(Properties props) {
    		this.props = props;
    	}
    }
    
             
  3. Configure PropertiesDemo class into Spring-Core.xml configuration file.
  4. <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-3.0.xsd">
    
    	<bean id="propDemo" class="com.java2learn.core.PropertiesDemo">
    		<property name="props">
    			<props>
    				<prop key="one">JAVA</prop>
    				<prop key="two">PHP</prop>
    			</props>
    		</property>
    	</bean>
    </beans> 
             
  5. Create client and run it.
  6. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.java2learn.core.PropertiesDemo;
    
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-Core.xml");
    		PropertiesDemo prop = (PropertiesDemo) applicationContext.getBean("propDemo");
    		System.out.println(prop.getProps());
    
    	}
    }
    
             

Output:

{two=PHP, one=JAVA}