• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Merge Collections in Spring?

November 9, 2013 //  by Krishna Srinivasan//  Leave a Comment

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

Category: Spring FrameworkTag: Spring IOC

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.

Previous Post: « Spring Collections Example
Next Post: Bean Definition Inheritance in Spring »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact