1

I have two enumerateds:

public enum One implements Representable{
   ONE
}
public enum Two implements Representable{
   TWO
}

Now, I need to declare the following spring-bean:

<bean id="listGeneratorContainer" class="pack.age.Container>
    <property name="generators">
        <map key-type="pack.age.Representable"> <!-- How to specuify the Type? -->
            <entry key="ONE" value="1"/>
            <entry key="TWO" value="2"/>
        </map>
    </property>
</bean>

where

package pack.age;

public class Container{
    private Map<Representable, Interger> generators
    //GET, SET, staff
}

Is it possible to tell spring to inject enums of the different types? In the case of a single enum that's clear.

It's not working now:

 java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [pack.age.Representable]: no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:219)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:362)
    ... 34 more
St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

1

I think you are looking for the way to embed Enum as keys in spring map definition. Here is an example post for the same.

<util:map id="myMap">
  <entry key="#{T(com.acme.MyEnum).ELEM1}" value="value1" />
  <entry key="#{T(com.acme.MyEnum).ELEM2}" value="value2" />
</util:map>

Make sure you reference it like above. So in your case it would be something like this.

<bean id="listGeneratorContainer" class="pack.age.Container>
<property name="generators">
    <map key-type="pack.age.Representable"> 
        <entry key="#{T(pack.age.ONE)ONE}" value="1"/>
        <entry key="#{T(pack.age.TWO)TWO}" value="2"/>
    </map>
</property>

Hope it helps.

Community
  • 1
  • 1