Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

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;
    }

    @Override
    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">
                <value>mobile</value>
                <value>mouse</value>
            </list>
        </property>
    </bean>
    <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>
            </list>
        </property>
    </bean>
</beans>


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);
        System.out.println(flipcart);
    }

}


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.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,spring beans ,collection merging

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}