相关疑难解决方法(0)

如何在Spring配置文件中为bean的属性分配一个Enum值?

我定义了一个独立的枚举类型,如下所示:

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}
Run Code Online (Sandbox Code Playgroud)

现在,我想将该类型的值注入bean属性:

<bean name="someName" class="my.pkg.classes">
   <property name="type" value="my.pkg.types.MyEnumType.TYPE1" />
</bean>
Run Code Online (Sandbox Code Playgroud)

......那不起作用:(

我应该如何将一个枚举注入一个春豆?

java spring

105
推荐指数
6
解决办法
12万
查看次数

Spring框架:使用util:map填充Map <Enum,Object>

我有这个工厂类,我想通过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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在使用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> 
Run Code Online (Sandbox Code Playgroud)

编辑

所以我重构了映射

<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> …
Run Code Online (Sandbox Code Playgroud)

java spring

14
推荐指数
2
解决办法
5万
查看次数

标签 统计

java ×2

spring ×2