Spring Framework:使用util:map </enum,object>填充Map <enum,object>

时间:2010-09-02 14:06:56

标签: java spring

我有这个工厂类,我想通过spring连接地图的运行时配置。该映射包含枚举对象和标准pojo。

public class GenericEntityFactoryImpl implements GenericEntityFactory
{
    private Map<IndexType,IEntity> indexEntityMap = null;

    @Override
    public IEntity getIndexEntity(IndexType index) {
        return indexEntityMap.get(index);
    }

    public Map<IndexType, IEntity> getIndexEntityMap() {
        return indexEntityMap;
    }

    public void setIndexEntityMap(Map<IndexType, IEntity> indexEntityMap) {
        this.indexEntityMap = indexEntityMap;
    }
}

我的spring util有问题:地图布线,因为在确定键值时我不确定如何正确引用特定的枚举类型。地图值的bean ref很简单。弹簧图布线的所有例子似乎都假设键是一个字符串!

<!-- the value object bean -->
<bean id="cell" class="com.xx.xx.common.index.entity.CellEntity"/>

<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl">
  <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">
           <entry key="CELL">
                <ref bean="cell"/>
            </entry>
       </util:map>
</bean> 

修改

所以我重构了映射

<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl" >
    <property name="indexEntityMap">
        <map >
            <entry key="com.xx.xx.common.index.CELL"><ref bean="cell"/></entry>
        </map>   
    </property>
</bean>

但假设春天会聪明失败......

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

4 个答案:

答案 0 :(得分:12)

通过定义我计划作为单独的bean添加到地图的每个枚举,我找到了一种解决方法 - 感谢creating-spring-bean-from-java-5-enum

<bean id="CELL" class="com.xx.xx.common.index.IndexType" factory-method="valueOf">
    <constructor-arg>
        <value>CELL</value>
    </constructor-arg>
</bean>

<bean id="APN" class="com.xx.xx.common.index.IndexType" factory-method="valueOf">
    <constructor-arg>
        <value>APN</value>
    </constructor-arg>
</bean>

定义了枚举后我可以在地图中对它们进行键控 -

<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl" >
    <property name="indexEntityMap">
        <map>
            <entry key-ref="CELL"><ref bean="cell"/></entry>
            <entry key-ref="APN"><ref bean="apn"/></entry>
        </map>   
    </property>
</bean>

答案 1 :(得分:10)

这是另一种更短的格式:

<bean id="versionService" class="my.service.VersionService" 
        p:animalDAOMap-ref="animalDAOMap"/>

<util:map id="p:animalDAOMap">
    <entry key="chicken" value-ref="chickenDAO"/>
    <entry key="monkey" value-ref="monkeyDAO"/>
    <entry key="pig" value-ref="pigDAO"/>
</util:map>

确保包含命名空间

xmlns:util="http://www.springframework.org/schema/util"

架构

http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd

顺便说一下,我在这里使用Spring 3.0+

答案 2 :(得分:0)

和你一样犯了同样的错误。我能够通过将我的枚举移动到独立文件并将我的枚举公开来避免它。

所以我的

public enum EventType { INFO, ERROR }

放在

  

EventType.java

如果重要的话,我只有一个包。 我正在以这种方式注入依赖(通过构造函数arg通过xml):

  

spring.xml:

<constructor-arg>
    <map>
        <entry key="INFO" value-ref="consoleEventLogger"></entry>
        <entry key="ERROR" value-ref="combinedEventLogger"></entry>
    </map>
</constructor-arg>

这对我来说是弹簧核心4.3.6

我相信有一个解释,这取决于反射和内部Spring逻辑。但我没有太多的Java经验,我无法提供。

答案 3 :(得分:-1)

我认为这就是你所需要的。注意我认为您不需要指定键类型和值类型属性。 Spring应该能够解决这个问题。

<bean id="genericEntityFactory" class="com.xx.xx.common.index.GenericEntityFactoryImpl">
  <property name="indexEntityMap" ref="indexEntityMapBean"/>
</bean>

<util:map id="indexEntityMapBean" 
       map-class="java.util.HashMap" 
       key-type="com.xx.xx.common.index.IndexType" 
       value-type="com.xx.xx.common.index.GenericEntityFactoryImpl">
           <entry key="com.xx.xx.common.index.IndexType.CELL">
                <ref bean="cell"/>
           </entry>
</util:map>

使用<util:map代替清洁的自治<map>(参见3.3.3.3部分)的唯一原因是,如果您想将相同的地图连接到多个地方,或者您想要使用一个不同的底层地图实现,例如ConcurrentHashMap。