Results 1 to 2 of 2

Thread: Cannot create copy of Collection type [java.util.ArrayList] - injecting original ...

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Default Cannot create copy of Collection type [java.util.ArrayList] - injecting original ...

    Hi,

    I am trying to inject a list into a constructor that expects a java.util.List<java.math.BigDecimal> object as the argument. Here is my XML Spring config:

    <bean id="myBean" class="MyClass">
    <constructor-arg>
    <list>
    <value>0.913291139</value>
    <value>0.086708861</value>
    </list>
    </constructor-arg>
    </bean>

    Here is an excerpt of MyClass:

    public class MyClass implements MyInterface {

    public MyClass(List<BigDecimal> numbers) {
    this.numbers = new ArrayList<BigDecimal>(numbers);
    }

    ....
    }


    However, when my app starts up, I get the following exception:

    org.springframework.beans.TypeConverterDelegate: Cannot create copy of Collection type [java.util.ArrayList] - injecting original Collection as-is { java.lang.InstantiationException: mypackage.MyInterface
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at org.springframework.beans.TypeConverterDelegate.co nvertToTypedCollection(TypeConverterDelegate.java: 556)
    at org.springframework.beans.TypeConverterDelegate.co nvertIfNecessary(TypeConverterDelegate.java:231)
    at org.springframework.beans.TypeConverterDelegate.co nvertIfNecessary(TypeConverterDelegate.java:104)

    I looked at TypeConverterDelegate. It looks like it tried to make a copy of the list by calling newInstance on java.util.List.class but it failed because java.util.List is abstract.

    Does anyone know what is the proper way of injecting a list into a constructor?

    Many thanks.

    Calvin

  2. #2

    Cool

    Hi Calvin,

    Think that it should be working because it worked for me,

    Please try to creating only a spring beans with constructor injection of list in another project and see what happens.

    It could be some other factors giving you this error.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •