Wednesday 15 March 2017

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
 <bean id="parent" abstract="true" class="com.sample.pojo.Organization">
  <property name="orgEmails">
   <props>
    <prop key="administrator">administrator@example.com</prop>
    <prop key="support">support@example.com</prop>
   </props>
  </property>
 </bean>

 <bean id="child" parent="parent">
  <property name="orgEmails">
   <!-- the merge is specified on the child collection definition -->
   <props merge="true">
    <prop key="sales">sales@example.com</prop>
    <prop key="support">support@example.co.in</prop>
   </props>
  </property>
 </bean>

Notice the use of the merge=true attribute on the <props/> 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 <props/>, 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
<?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="parent" abstract="true" class="com.sample.pojo.Organization">
  <property name="orgEmails">
   <props>
    <prop key="administrator">administrator@example.com</prop>
    <prop key="support">support@example.com</prop>
   </props>
  </property>
 </bean>

 <bean id="child" parent="parent">
  <property name="orgEmails">
   <!-- the merge is specified on the child collection definition -->
   <props merge="true">
    <prop key="sales">sales@example.com</prop>
    <prop key="support">support@example.co.in</prop>
   </props>
  </property>
 </bean>

</beans>

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.
support : support@example.co.in
administrator : administrator@example.com
sales : sales@example.com

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

No comments:

Post a Comment