Spring Рамка: заполнение карты <Enum, Object> с использованием: map
У меня есть этот класс factory, который я хочу провести через 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: map, так как я не уверен, как правильно ссылаться на определенный тип перечисления при определении значения ключа. bean ref для значения карты легко. Все примеры проводки карты spring, похоже, предполагают, что ключ является строкой!
<!-- 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>
но предположение, что spring будет умным сбой...
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
Ответы
Ответ 1
Я нашел обходное решение, определяя каждый enum, который я планирую добавить к карте как отдельный 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>
Ответ 2
Здесь альтернативный и более короткий формат:
<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+ здесь
Ответ 3
Получается та же ошибка, что и вы. Я смог избежать этого, переместив свой enum в автономный файл и сделав свой enum общедоступным.
Итак, мой
public enum EventType { INFO, ERROR }
Расположены в
EventType.java
Также у меня есть только один пакет, если это имеет какое-то значение.
Я ввожу зависимость таким образом (через xml by constructor arg):
spring.xml:
<constructor-arg>
<map>
<entry key="INFO" value-ref="consoleEventLogger"></entry>
<entry key="ERROR" value-ref="combinedEventLogger"></entry>
</map>
</constructor-arg>
Это работает для меня с spring -core 4.3.6
Я считаю, что есть объяснение, которое зависит от рефлексии и внутренней логики Spring. Но у меня мало опыта Java, и я не могу этого гарантировать.
Ответ 4
Я думаю, что это то, что вам нужно. Заметьте, я не думаю, что вам нужно указать атрибуты типа ключа и значения. 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.