Collection merging in Spring

With this example we will show you how to merge Collections in Spring.

Since Spring 2.0, the container supports merging of collections. You can define <prop/>, <list/>, <map/> and <set/> of parent-style and then you can inherit the child-style element and override values of each type from parent collections. That is, the child collection’s values are the result of merging the elements of the parent and child collections, with the child’s collection elements overriding values specified in the parent collection.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following things are required in order to run the application
Eclipse Kepler
JDK 1.8
Have maven 3 installed and configured
Spring dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : spring-collection-merge
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
        <!-- Spring framework -->
        <!-- junit -->

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Step 5. Create an XML properties file under src/main/resources.

Do right-click on src/main/resources in the project and go New -> file. Give File name: as applicationContext.xml and click on Finish button.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="springCollectionParent" class="com.roytuts.spring.collection.SpringCollectionMerge">
        <property name="properties">
                <prop key="administrator"></prop>
                <prop key="support"></prop>
                <prop key="development"></prop>
                <prop key="sitenv"></prop>
                <prop key="uatenv"></prop>
    <bean id="springCollectionChild" parent="springCollectionParent">
        <property name="properties">
            <!-- the merge is specified on the *child* collection definition -->
            <props merge="true">
                <prop key="administrator"></prop>
                <prop key="support"></prop>
                <prop key="sitenv"></prop>
                <prop key="uatenv"></prop>

Step 6. Create a POJO class that contains Properties element

package com.roytuts.spring.collection;
import java.util.Properties;
public class SpringCollectionMerge {
    private Properties properties;
    public Properties getProperties() {
        return properties;
    public void setProperties(Properties properties) { = properties;

Step 7. Create a Test class which will test our code how it works

package com.roytuts.spring.collection;
import java.util.Map.Entry;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
public class SpringCollectionMergeTest {
    private SpringCollectionMerge springCollectionMerge;
    public void setUp() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        springCollectionMerge = applicationContext.getBean(
                "springCollectionChild", SpringCollectionMerge.class);
    public void displayProperties() {
        Properties properties = springCollectionMerge.getProperties();
        for (Entry<Object, Object> entry : properties.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());

Step 8. Run the above test class, you will get below output

uatenv =>
support =>
administrator =>
development =>
sitenv =>

Please use merge=”true” in the child bean in metadata configuration file to merge the parent collections with child collections.

The child Properties collection’s value set inherits all property elements from the parent <props/>,
and the child’s values for administrator, support, uatenv and sitenv override the values in the parent collection.

That’s all. Thanks for reading.

Leave a Reply

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