Home Tutorial Spring Spring3 Ioc Spring Collection Merging

 
 

Share on Google+Share on Google+
Spring Collection Merging
Posted on: August 19, 2010 at 12:00 AM
Advertisement
In this tutorial you will see an example of spring collection merging.

Spring Collection Merging

The merging of collection is allowed in the Spring 2.0. A parent-style like <list/>, <map/>, <set/> or <pros/> element can have child-style <list/>, <map/>, <set/> or <pros/> element which inherit and overrides the values from the parent collection. For collection merging you need to specify merge=true attribute on the <props/> element of child bean definition. Note you cannot merge Map and List collection type.

CollegeBean.java

package collection.merge.example;

import java.util.Properties;

public class CollegeBean {
  private Properties pros;

  public Properties getPros() {
    return pros;
  }

  public void setPros(Properties pros) {
    this.pros = pros;
  }

  @Override
  public String toString() {
    return "College [Props=" + pros + "]";
  }
}

ProsMain.java

package collection.merge.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PropsMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
        "context.xml");
    CollegeBean coll = (CollegeBeanbeanfactory.getBean("child");
    System.out.println(coll);
  }
}

context.xml

<bean id="parent" abstract="true" 
class
="collection.merge.example.CollegeBean">
  <property name="pros">
    <props>
      <prop key="Year">25</prop>
      <prop key="NickName">Raj</prop>
    </props>
  </property>
</bean>

<bean id="child" parent="parent" >
  <property name="pros">
    <props merge="true">
      <prop key="Roll">101</prop>
      <prop key="Name">Rohit Raj</prop>
    </props>  
  </property>
</bean>  
</beans>

When you run this application it will display output as shown below:


College [Props={Name=Rohit Raj, Roll=101, Year=25, NickName=Raj}]

Download this example code

Advertisement

Related Tags for Spring Collection Merging:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 19, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com