Announcement Announcement Module
Collapse
No announcement yet.
Combining multiple lists into one Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining multiple lists into one

    Is there a way to combine multiple lists into one within a spring xml configuration? This would be similar to invoking the addAll method on java.util.List.

    I tried the following:

    Code:
       <bean id="list1" class="java.util.ArrayList">
            <constructor-arg>
                <list>
                    <value>test</value>
                    <value>test</value>
                </list>
            </constructor-arg>
        </bean>
        
        
        <bean id="list2" class="java.util.ArrayList">
            <constructor-arg>
                <list>
                    <value>test</value>
                    <value>test</value>
                </list>
            </constructor-arg>
        </bean>
        
        <bean id="combinedList" class="java.util.ArrayList">
            <constructor-arg>
                <list>
                    <ref local="list1"/>
                    <ref local="list2"/>
                </list>
            </constructor-arg>
        </bean>
    This creates a list of lists. This makes sense, but I need the combinedList to contain only the elements from list1 and list2.

    Thanks.

    -karl

  • #2
    I am now aware of a built-in way to support this scenario, but you can use a user FactoryBean as follows:
    Code:
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.beans.factory.FactoryBean;
    
    /**
     * @author <a href="mailto:[email protected]">Omar Irbouh</a>
     * @since 2005.10.23
     */
    public class ListMergerFactory implements FactoryBean {
      private List list = new ArrayList();
    
      public ListMergerFactory(List<List> items) {
        for (List item : items) {
          if (item != null) {
            list.addAll(item);
          }
        }
      }
    
      public Object getObject() throws Exception {
        return list;
      }
    
      public Class getObjectType() {
        return list.getClass();
      }
    
      public boolean isSingleton() {
        return true;
      }
    
    }
    b/w, what is the exact use-case for this?

    Comment


    • #3
      Thanks. This is exactly what i ended up doing. Just seemed like a FactoryBean that might come with spring out of box.

      Regarding the use case, we have 3 teams, each running their service within the same EAR. We are using Spring/Hibernate with one hibernate session factory. Each team defines it's own hibernate mappings list within their own application context. We then combine those individual lists into one, by using a custom factory bean like the one in your example. The resulting list of mappings is injected into spring's LocalSessionFactoryBean.

      Thanks for your help.

      -karl

      Comment

      Working...
      X