Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring: Collections merging

Spring API provides support to Merge Collections. You can define a bean such that, the Collection of elements of child bean can be inherited form parent bean.

Example
 id="parent" abstract="true" class="com.sample.pojo.Organization">
name="orgEmails">

key="administrator">[email protected]
key="support">[email protected]




id="child" parent="parent">
name="orgEmails">

merge="true">
key="sales">[email protected]
key="support">[email protected]




Notice the use of the merge=true attribute on the element of the orgEmails property of the child bean definition. When the child bean is resolved and instantiated by the container, the resulting instance has an orgEmails Properties collection that contains the result of the merging of the child’s orgEmails collection with the parent’s orgEmails collection.

The child Properties collection’s value set inherits all property elements from the parent , and the child’s value for the support value overrides the value in the parent collection.

Following is the complete working application.

Organization.java
package com.sample.pojo;

import java.util.Properties;

public class Organization {
private Properties orgEmails;

public Properties getOrgEmails() {
return orgEmails;
}

public void setOrgEmails(Properties orgEmails) {
this.orgEmails = orgEmails;
}

}

myConfiguration.xml

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">

id="parent" abstract="true" class="com.sample.pojo.Organization">
name="orgEmails">

key="administrator">[email protected]
key="support">[email protected]




id="child" parent="parent">
name="orgEmails">

merge="true">
key="sales">[email protected]
key="support">[email protected]






HelloWorld.java
package com.sample.test;

import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Organization;

public class HelloWorld {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

Organization organization = context.getBean("child", Organization.class);

Properties props = organization.getOrgEmails();

for(Object property: props.keySet()){
System.out.println(property + " : " + props.get(property));
}

((ClassPathXmlApplicationContext) context).close();
}
}

Run HelloWorld.java, you can able to see following output.

In the same way you can apply merging to all supported collecitons like List, Map, Set.

Can I merge Collections of different types?

No, you can’t merge collections of different types. For example, if you try to merger Map collection to List, you will get an exception.









Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring: Collections merging

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×