Over a million developers have joined DZone.

Collection Merging in Spring [Snippets]

DZone's Guide to

Collection Merging in Spring [Snippets]

This overview of collection merging will help bring different Spring beans' data together with the help of three guidelines.

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

The merging of one bean property collection that's dependent on another bean property collection dependent is called a collection merge.

Let's assume that I am purchasing some items through Flipkart. In this example, I've added some items that I want to buy to my cart. Let's see how we add items to my cart in Flipkart with a sample "Flipcart" class.

package com.cm.beans;

import java.util.List;

public class Flipcart {
    private String user;
    private List < String > items;

    public void setUser(String user) {
        this.user = user;

    public void setItems(List < String > items) {
        this.items = items;

    public String toString() {
        return "Flipcart [user=" + user + ", items=" + items + "]";


And here's my Spring bean configuration file — application-context.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.xsd">
    <bean id="cart1" class="com.cm.beans.Flipcart">
        <property name="user" value="john"></property>
        <property name="items">
            <list value-type="java.lang.String">
    <bean id="cart2" class="com.cm.beans.Flipcart" parent="cart1">
        <property name="items">
            <list value-type="java.lang.String" merge="true">
                <value>Curren Analog Watch</value>
                <value>Nikon Coolpix Camera </value>

And a test class:

package com.cm.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.cm.beans.Flipcart;

public class FlipcartTest {

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/cm/common/application-context.xml"));
        Flipcart flipcart = factory.getBean("cart2", Flipcart.class);


Here, we're merging the cart1 and cart2 beans to the same bean. If I didn't use merge="true", then the IOC container would overwrite the property, those properties having the same name.

For collection merging, we have to follow three principles:

  1. The beans must and should follow bean inheritance.

  2. The bean collection implementation classes must be the same.

  3. The collection implementation class's generic type must be the same.

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

java ,spring beans ,collection merging

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}