JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

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

[code lang=”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;
}
}
[/code]

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

spring-beans-ioc.xml

[code lang=”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>
[/code]

3. Load Spring Configuration

SpringContextLoader.java

[code lang=”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();
}
}
[/code]

Filed Under: Spring Framework Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved