简体   繁体   中英

Collection Merge in Java Spring using beans

I am new to Spring Framework.I am trying to merge two sets using spring. Beans.xml

<bean name = "mainAccount" class="java.util.HashSet">
    <constructor-arg>
        <set>
            <value>123</value>
            <value>1234</value>
        </set>
    </constructor-arg>
</bean> 

<bean name = "subAccount" class="java.util.HashSet" parent="mainAccount">
    <constructor-arg> 
        <set merge="true"> 
            <value>231</value>
            <value>23221</value>
        </set>
    </constructor-arg>
</bean>     
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
   <constructor-arg name="AccountIDs" ref="subAccount" />
</bean>

I am not able to merge the sets in to the subAccount set. I am getting the exception :-

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'subAccount' defined in class path resource [Beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Could not convert constructor argument value of type [java.util.LinkedHashSet] to required type [int]: Failed to convert value of type 'java.util.LinkedHashSet' to required type 'int'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.util.LinkedHashSet] to required type [int]: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type [java.util.LinkedHashSet]

My HelloWorld.java is :-

package com.tutorialspoint;
import java.util.HashSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class HelloWorld {
  private final Set<String>AccountIDs;

  public HelloWorld(Collection<String> AccountIDs){
    this.AccountIDs = new HashSet<String>(AccountIDs);  
    Iterator iterator = AccountIDs.iterator();
    while (iterator.hasNext())
      System.out.print( iterator.next() + ", " );
      System.out.println();
   }
}

When I pass parent bean set "mainAccount" then it is showing the set values properly but when I try to pass the child bean "subAccount" I got the error.I want to merge the mainAccount to subAcount.I tried all posssible ways but not able to solve this.Some guidance would be helpful.

The issue here that i see is, the way you are trying to merge the collection. You are creating a instance of a collection and hoping that the merging will occur during construction of the Set.

How spring merging works is, you will have to define your own bean (Parent) with a property of collection say Set with property name say mySet, and then you can define your child bean with the same property name and declare the set's merge attribute to true. Thats when spring will understand which collection to merge with what.

You can follow below URL to see how its done

Collection Merging

Updated Beans.Xml. I have to add index.

<bean name = "mainAccount" class="java.util.HashSet">
  <constructor-arg index="0">
    <set>
        <value>123</value>
        <value>1234</value>
    </set>
  </constructor-arg>
</bean> 

<bean name = "subAccount" class="java.util.HashSet" parent="mainAccount">
  <constructor-arg index="0"> 
    <set merge="true"> 
        <value>231</value>
        <value>23221</value>
    </set>
  </constructor-arg>
</bean>     
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
  <constructor-arg name="AccountIDs" ref="subAccount" />
 </bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM