Collections in Spring

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Collections List, Set, Map and Properties can be injected in a Spring Bean. Below example to show you how to inject values into collections type (List, Set, Map, and Properties).

CollectionEntity.java

package com.javatutsworld.common;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionEntity {
	
	private List<Object> list;

	private Set<Object> set;
	
	private Map<Object, Object> map;

	private Properties props;

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public Set<Object> getSet() {
		return set;
	}

	public void setSet(Set<Object> set) {
		this.set = set;
	}

	public Map<Object, Object> getMap() {
		return map;
	}

	public void setMap(Map<Object, Object> map) {
		this.map = map;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}
		
}

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="entity" class="com.javatutsworld.common.CollectionEntity"> 
		<property name="list">
		  <list>
		   <value>xyz</value>
		   <ref bean="userBean" />
		  </list>
		</property>
		<property name="set">
		  <set>
		   <value>abc</value>
		   <ref bean="userBean" />
		  </set>
		</property>
		<property name="map">
		  <map>
		   <entry key="1" value="hahaha" />
		   <entry key="2" value-ref="userBean" />
		  </map>
		</property>
		<property name="props">
		  <props>
		   <prop key="k1">100</prop>
		   <prop key="k2">200</prop>
		   <prop key="k3">300</prop>
		  </props>
		</property>
	</bean>
	
	<bean id="userBean" class=" com.javatutsworld.common.User">
		<property name="name" value="Aariz"/>
		<property name="id" value="1001"/>
	</bean>
</beans>

User.java

package com.javatutsworld.common;

public class User {

	private Long id;
	
	private String name;
	
	public void setId(Long id){
		this.id = id;
	}

	public void setName(String name){
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "User: [ " + name +", " + id + "]";
	}
}

Main.java

package com.javatutsworld.test;

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

import com.javatutsworld.common.HelloWorld;

public class Main {
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
	
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
			
		CollectionEntity collentity = (CollectionEntity) context.getBean("entity");
			
		System.out.println("List: " + collentity.getList());
			
		System.out.println("Set : " + collentity.getSet());
			
		System.out.println("Map : " + collentity.getMap());
			
		System.out.println("Props : " + collentity.getProps());
					
	}
}

Output

List: [abc, Person: [ Aariz, 1001]]
Set : [xyz, Person: [ Aariz, 1001]]
Map : {1=hahaha, 2=Person: [ Aariz, 1001]}
Props : {k3=300, k2=200, k1=100}
      Advertisements