title: Spring Framework : Populating a Map<Enum,Object> with util:map
body: <p>I have this factory class, which i want to wire via spring for runtime configuration of the map. The map contains an enum object and standard pojo. </p>
  
  <pre><code>public class GenericEntityFactoryImpl implements GenericEntityFactory
  {
      private Map&lt;IndexType,IEntity&gt; indexEntityMap = null;
  
      @Override
      public IEntity getIndexEntity(IndexType index) {
          return indexEntityMap.get(index);
      }
  
      public Map&lt;IndexType, IEntity&gt; getIndexEntityMap() {
          return indexEntityMap;
      }
  
      public void setIndexEntityMap(Map&lt;IndexType, IEntity&gt; indexEntityMap) {
          this.indexEntityMap = indexEntityMap;
      }
  }
  </code></pre>
  
  <p>I'm having trouble with my spring util:map wiring, since i'm not sure how to correctly reference a specific enum type when definiting the key value. The bean ref for the map value is easy. All the examples of spring map wiring seem to assume that the key is a string!</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!-- the value object bean --&gt;
  &lt;bean id="cell" class="com.xx.xx.common.index.entity.CellEntity"/&gt;
  
  &lt;bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl"&gt;
    &lt;util:map 
         id="indexEntityMap" 
         map-class="java.util.HashMap" 
         key-type="com.xx.xx.common.index.IndexType" 
         value-type="com.xx.xx.common.index.GenericEntityFactoryImpl"&gt;
             &lt;entry key="CELL"&gt;
                  &lt;ref bean="cell"/&gt;
              &lt;/entry&gt;
         &lt;/util:map&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>Edit</p>
  
  <p>So i refactored the mapping</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl" &gt;
      &lt;property name="indexEntityMap"&gt;
          &lt;map &gt;
              &lt;entry key="com.xx.xx.common.index.CELL"&gt;&lt;ref bean="cell"/&gt;&lt;/entry&gt;
          &lt;/map&gt;   
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>but assumption that spring will be smart fails...</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericEntityFactory' defined in class path resource [com/xx/xx/common/index/index-application-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'indexEntityMap'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.xx.xx.common.index.IndexType] for property 'indexEntityMap[com.xx.xx.common.index.CELL]': no matching editors or conversion strategy found
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
      at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
      ... 17 more
  Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'indexEntityMap'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.xx.xx.common.index.IndexType] for property 'indexEntityMap[com.xx.xx.common.index.CELL]': no matching editors or conversion strategy found
      at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1288)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1249)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
      ... 32 more
  Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.xx.xx.common.index.IndexType] for property 'indexEntityMap[com.xx.xx.common.index.CELL]': no matching editors or conversion strategy found
      at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:219)
      at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:508)
      at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:194)
      at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
      at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
      ... 36 more
  </code></pre>