Spring Collections (List, Set, Map, and Properties) example

Spring examples to show you how to inject values into collections type (List, Set, Map, and Properties). 4 major collection types are supported :

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans



package collections;


import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;


public class Customer {


private List<String> phones;

private Set<String> address ;

private Map<String,String>course;

private Properties pros;

@Override

public String toString() {

return "Customer [phones=" + phones + ", address=" + address + ", course=" + course + ", pros=" + pros + "]";

}

public List<String> getPhones() {

return phones;

}

public void setPhones(List<String> phones) {

this.phones = phones;

}

public Set<String> getAddress() {

return address;

}

public void setAddress(Set<String> address) {

this.address = address;

}

public Map<String, String> getCourse() {

return course;

}

public void setCourse(Map<String, String> course) {

this.course = course;

}

public Properties getPros() {

return pros;

}

public void setPros(Properties pros) {

this.pros = pros;

}

public Customer(List<String> phones, Set<String> address, Map<String, String> course, Properties pros) {

super();

this.phones = phones;

this.address = address;

this.course = course;

this.pros = pros;

}

public Customer() {

super();

// TODO Auto-generated constructor stub

}


}

See different code snippets to declare collection in bean configuration file.

1. List example


	<property name="phones">

<list>

<value>1</value>

<value>2</value>

<value>3</value>

<value>4</value>

<value>5</value>

</list>

</property>

2. Set example


<property name="address">

<set>

<value>adsfd</value>

<value>dsfv</value>

<value>dsf</value>

<value>df</value>

<value>dvf</value>

</set>

</property>

3. Map example


	<property name="course">

<map>

<entry key="java" value="2month"></entry>

<entry key="c" value="1month"></entry>

<entry key="cpp" value="3month"></entry>

<entry key="python" value="1month"></entry>

<entry key="javasacript" value="2 month"></entry>


</map>

4. Properties example


	</property>	

<!-- java.util.Properties -->

<property name="pros">

<props>

<prop key="admin">admin@nospam.com</prop>

<prop key="support">support@nospam.com</prop>

</props>

</property>

Full Spring’s bean configuration file.

<?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-2.5.xsd">


<bean class="collections.Customer" name="collection">

<property name="phones">

<list>

<value>1</value>

<value>2</value>

<value>3</value>

<value>4</value>

<value>5</value>

</list>

</property>

<property name="address">

<set>

<value>adsfd</value>

<value>dsfv</value>

<value>dsf</value>

<value>df</value>

<value>dvf</value>

</set>

</property>

<property name="course">

<map>

<entry key="java" value="2month"></entry>

<entry key="c" value="1month"></entry>

<entry key="cpp" value="3month"></entry>

<entry key="python" value="1month"></entry>

<entry key="javasacript" value="2 month"></entry>


</map>


</property>

<!-- java.util.Properties -->

<property name="pros">

<props>

<prop key="admin">admin@nospam.com</prop>

<prop key="support">support@nospam.com</prop>

</props>

</property>

</bean>


</beans>

Run it…


package collections;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App {


public static void main(String[] args) {

// TODO Auto-generated method stub


ApplicationContext context = new ClassPathXmlApplicationContext("collections/collectionConfig.xml");


Customer cust = (Customer)context.getBean("collection");

System.out.println(cust.getAddress());

System.out.println(cust.getCourse());

System.out.println(cust.getPhones());

System.out.println(cust.getPros());

}


}

Output


[adsfd, dsfv, dsf, df, dvf]

{java=2month, c=1month, cpp=3month, python=1month, javasacript=2 month}

[1, 2, 3, 4, 5]

{admin=admin@nospam.com, support=support@nospam.com}

] 

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence