How to Merge Collections in Spring?

In my previous post I have explained about how to use collections in Spring. This tutorial explains how to merge collections or inherit the values from the parent bean. In some scenarios we need to define a bean as abstract using the abstract=true in the bean definition. It means that another bean definition will be child for this parent bean and will inherit all its properties. This is very useful when we have a common behavior which is used by most of the implementations. In that scenario, spring provides option to merge or inherit the collections which is defined in the parent bean. For example : If you define a List in your parent bean A, another child bean B can point bean A as parent and inherit all the list values defined in that bean.

There are some limitations on merging two collections. We can not merge two different types of collections. For example : we can not merge List and Set. This merging feature is available from Spring 2.0 on wards.

 

Look at the below example :

1. Create a Parent Bean

Company.java

package javabeat.net.core.ioc;

import java.util.List;

public class Company {
	private List<String> employees;

	public List<String> getEmployees() {
		return employees;
	}

	public void setEmployees(List<String> employees) {
		this.employees = employees;
	}
}

2. Define Child Bean and Parent Bean in the Spring Configuration

spring-beans-ioc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:component-scan base-package="javabeat.net.core.ioc"/>
	<bean id="companyBean" abstract="true" class="javabeat.net.core.ioc.Company">
		<property name="employees">
			<list>
				<value>Employee 1</value>
				<value>Employee	2</value>
			</list>
		</property>
	</bean>
	<bean id="companyBeanChild" parent="companyBean">
		<property name="employees">
			<list merge="true">
				<value>Employee	3</value>
				<value>Employee	4</value>
			</list>
		</property>
	</bean>
</beans>

3. Load Spring Configuration

SpringContextLoader.java

package javabeat.net.core.ioc;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextLoader {
	public static void main (String args[]){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml");
		Company company = (Company)applicationContext.getBean("companyBeanChild");
		System.out.println(company.getEmployees());
		applicationContext.close();
	}
}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*