How to define EnumMap in Spring 3.0

I am trying to define EnumMap in Spring with. I tried the following options

<util:map map-class="java.util.EnumMap" key-type="xyz.EnumType"> <entry key="SOME_ENUM_TYPE"> <ref bean="someBean"/> </entry> </util:map> 

I get the following error

 Error creating bean with name 'util:map#1c599b0e': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.EnumMap]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.util.EnumMap.<init>() 

The following definition is what I tried first

 <util:map map-class="java.util.EnumMap"> <entry key="SOME_ENUM_TYPE"> <ref bean="someBean"/> </entry> </util:map> 

and this gave me some error due to the inability to assign enumtype to String.

There are examples of using a shared map on the site, but I'm trying to check if I can use EnumMap, since it is considered the most optimal for Enums. The answer can be very obvious, so my apologies if the question is stupid. This is probably due to my limited knowledge of Spring. Thanks

+7
source share
2 answers

I think you cannot initialize EnumMap with <util:map> . However, EnumMap has a constructor that accepts an existing Map , you can try using it:

 <bean class = "java.util.EnumMap"> <constructor-arg> <util:map key-type="xyz.EnumType"> <entry key="SOME_ENUM_TYPE"><ref bean="someBean"/></entry> </util:map> </constructor-arg> </bean> 
+12
source

This problem is because EnumMap does not have a default constructor without an argument. Spring will be init with no argument if no error exists.

0
source

All Articles