• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Spring Collections Example

November 9, 2013 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explains how to use the collections API with Spring configurations. Spring provides out of the box support for all the collections classes like List, Set, Map and Properties for injecting the objects. There is a corresponding elements in the spring configuration file to pass the values to the Java objects. This tutorial passes simple string objects to the collections classes, however you can use the custom objects to store in the collections. Lets look at the example below.

1. Spring Bean and Collections

SpringCollectionsExample.java

[code lang=”java”] package javabeat.net.core.ioc;

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

public class SpringCollectionsExample {
private List<Object> lists;
private Set<Object> sets;
private Map<Object, Object> maps;
private Properties pros;
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Set<Object> getSets() {
return sets;
}
public void setSets(Set<Object> sets) {
this.sets = sets;
}
public Map<Object, Object> getMaps() {
return maps;
}
public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}

}
[/code]

2. Spring Configuration with Collections

spring-beans-ioc.xml

[code lang=”xml”] <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="javabeat.net.core.ioc"/>
<bean id="collectionsExampleBean" class="javabeat.net.core.ioc.SpringCollectionsExample">
<property name="lists">
<list>
<value>List Object 1</value>
<value>List Object 2</value>
</list>
</property>
<property name="sets">
<set>
<value>Set Object 1</value>
<value>Set Object 2</value>
</set>
</property>
<property name="maps">
<map>
<entry key="Key 1" value="Value 1"/>
<entry key="Key 2" value="Value 2"/>
<entry key="Key 3" value="Value 3"/>
</map>
</property>
<property name="pros">
<props>
<prop key="Prop Key 1">Value 1</prop>
<prop key="Prop Key 2">Value 2</prop>
</props>
</property>
</bean>
</beans>
[/code]

3. Spring Context Loader and Access Collections

SpringContextLoader.java

[code lang=”java”] package javabeat.net.core.ioc;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextLoader {
public static void main (String args[]){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml");
SpringCollectionsExample collectionsExample = (SpringCollectionsExample)applicationContext.getBean("collectionsExampleBean");
System.out.println(collectionsExample.getLists());
System.out.println(collectionsExample.getSets());
System.out.println(collectionsExample.getMaps());
System.out.println(collectionsExample.getPros());
applicationContext.close();
}
}
[/code]

If you run the application you would see the following result:

[code] [List Object 1, List Object 2] [Set Object 1, Set Object 2] {Key 1=Value 1, Key 2=Value 2, Key 3=Value 3}
{Prop Key 2=Value 2, Prop Key 1=Value 1}
[/code]

If you have any thoughts, please share it in the comments section.

Category: Spring FrameworkTag: Spring IOC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Circular Dependency in Spring
Next Post: How to Merge Collections in Spring? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact