Home  >  Spring Core

Example of Collection Merging in Spring

By Arvind Rai, April 10, 2013
In spring, java collections can be injected in bean using application xml. According to the need, we can merge collection data while creating bean. There will be a parent and child bean. Child will inherit the parent data. It is done by the attribute merge="true". This attribute is available in all collection tags like <list/>, <set/>, <map/> and <props/> . The child bean will use parent attribute to inherit parent bean data. The child bean collection tag will use merge="true" attribute to inherit parent collection elements. When we access child bean, we will get elements of child as well as parent bean collection. This is collection merging of beans in spring. To understand about spring collection injection, find the link. On this page we will provide example for spring collection merging.

Collection Merging using merge="true" using XML

spring-config.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="parentCollection" class="com.concretepage.bean.MyCollection">
       <property name="mySet">
	 <set>
             <value>AAAA</value>
             <value>BBBB</value>
         </set>
       </property>
    </bean>
    <bean id="childCollection" parent="parentCollection">
       <property name="mySet">
	 <set merge="true">
             <value>CCCC</value>
             <value>DDDD</value>
         </set>
       </property>
    </bean>
</beans> 



Create Bean

MyCollection.java
package com.concretepage.bean;
import java.util.Set;
public class MyCollection {
	private Set<String> mySet;
	public Set<String> getMySet() {
		return mySet;
	}
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet;
	}
} 

Run Application

SpringDemo.java
package com.concretepage;
import java.util.Iterator;
import java.util.Set;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.MyCollection;
public class SpringDemo {
	public static void main(String[] args) {
	AbstractApplicationContext  context = new ClassPathXmlApplicationContext("spring-config.xml");
	MyCollection myCollection=(MyCollection)context.getBean("parentCollection");
        //access parent collection 
        System.out.println("---Elements in parent bean---");		
        Set<String> parentSet=myCollection.getMySet();
        Iterator<String> itrP= parentSet.iterator();
        while(itrP.hasNext()){
        	System.out.println(itrP.next());
        }
        MyCollection concreteChild=(MyCollection)context.getBean("childCollection");
        //access child collection 
        System.out.println("---Elements in child bean---");        
        Set<String> setC=concreteChild.getMySet();
        Iterator<String> itrC= setC.iterator();
        while(itrC.hasNext()){
        	System.out.println(itrC.next());
        } 
        context.close();
	}
} 

Output

Find the output.
---Elements in parent bean---
AAAA
BBBB
---Elements in child bean---
AAAA
BBBB
CCCC
DDDD 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us