3

I have a class with the following constructor:

public MyClass(Map<String s, Collection<Object>> co){
    //impl
}

I need to invoke it via Spring bean's declarations. I tried to do something like this:

<bean id="toInject" class="path.toSomClass" />

<bean id="myBean" class="path.to.MyClass">
    <constructor-arg>
        <map>
            <entry key="String" 
        value-ref="_WHAT_SHOULD_I_WRITE_HERE_TO_DECLARE_A_COLLECTION_FROM_THE_ONLY_toInject">
        </map>
    </constructor-arg>
</bean>

I'm not concerned about the type of the Collection (It may be a List<T> as well as Set<T>, or anything else implementing Collection<T>).

Tunaki
  • 132,869
  • 46
  • 340
  • 423
St.Antario
  • 26,175
  • 41
  • 130
  • 318

2 Answers2

1

You can declare a <list> element as part of the value for the entry, like this:

<bean id="myBean" class="path.to.MyClass">
    <constructor-arg>
        <map>
            <entry key="String">
                <list>
                    <ref bean="toInject" />
                </list>
            </entry>
        </map>
    </constructor-arg>
</bean>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

Dependency injection happens at runtime, so no type arguments exist anymore at that time.

This configuration should work:

<bean id="myBean" class="path.to.MyClass">
    <constructor-arg>
        <map>
            <entry key="String">
                <list>
                     <value>Your value here, or</value>
                     <ref bean="yourBeanHere" />
                </list>
            </entry>
        </map>
    </constructor-arg>
</bean>
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43