使用属性HashMaps <string,class =“”> </string,>从bean中注入类

时间:2015-03-05 22:46:50

标签: java spring dictionary enums

我希望通过弹簧注入来填充具有两个HashMap类型的HashMaps的类。不幸的是,当我尝试构建我的平台时,我收到以下错误。

    SEVERE: Exception sending context initialized event to listener instance of class de.hybris.platform.spring.HybrisContextLoaderListener
org.springframework.beans.FatalBeanException: Context hybris Global Context Factory  couldn't  be created correctly due to, Error creating bean with name 'EnumUtil' defined in class path resource [payment3dsi-spring.xml]: Cannot create inner bean 'util:map#4b13c655' of type [org.springframework.beans.factory.config.MapFactoryBean] while setting bean property 'stateCodes'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'util:map#4b13c655': Error converting typed String value for bean property 'sourceMap' with key [TypedStringValue: value [US-IL], target type [class java.lang.String]]; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub$StateProvinceCode'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub$StateProvinceCode]: no matching editors or conversion strategy found

我不确定为什么会出现转换错误?我应该设置一个键/值对,不需要转换。我的bean看起来像这样:

    <bean name="EnumUtil" class="com.gsk.platform.payment3dsi.util.EnumUtil" scope="tenant">
    <property name="stateCodes">
        <util:map map-class="java.util.HashMap" key-type="java.lang.String" value-type="com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.StateProvinceCode">
            <entry key="US-IL" value="StateProvinceCode.Illinois" />
            .
            .
            .....etc
        </util:map>
    </property>
    <property name="countryCodes">
        <util:map map-class="java.util.HashMap" key-type="java.lang.String" value-type="com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.CountryCode">
            <entry key="US" value="CountryCode.UnitedStates" />
            <entry key="CA" value="CountryCode.Canada" />
        </util:map>
    </property>
</bean>

My Enum类如下所示:

package com.gsk.platform.payment3dsi.util;

import java.util.HashMap;
import java.util.Map.Entry;

import com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.CountryCode;
import com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.StateProvinceCode;

public class EnumUtil {
    private HashMap<String, StateProvinceCode> stateCodes;
    private HashMap<String, CountryCode> countryCodes;

    private HashMap<String, StateProvinceCode> getStateCodes() {
        return stateCodes;
    }

    private HashMap<String, CountryCode> getCountryCodes() {
        return countryCodes;
    }

    public void setStateCodes(HashMap<String, StateProvinceCode> stateCodes) {
        this.stateCodes = stateCodes;
    }

    public void setCountryCodes(HashMap<String, CountryCode> countryCodes) {
        this.countryCodes = countryCodes;
    }

    public String getRegionCodeForStateCode(StateProvinceCode stateProvinceCode){
        for(Entry<String, StateProvinceCode> entry : stateCodes.entrySet()){
            if(entry.getValue().equals(stateProvinceCode)){
                return entry.getKey();
            }
        }
        return null;
    }

    public StateProvinceCode getStateCodeFromRegionCode(String regionCode){
        return getStateCodes().get(regionCode);
    }

    public String getCountryIsoForCountryCode(CountryCode countryCode){
        for(Entry<String, CountryCode> entry : countryCodes.entrySet()){
            if(entry.getValue().equals(countryCode)){
                return entry.getKey();
            }
        }
        return null;
    }

    public CountryCode getCountryCodeFromCountryIso(String countryCode){
        return getCountryCodes().get(countryCode);
    }
}

我尝试以下列方式在我的代码中使用此类:

    @Resource(name = "EnumUtil")
    private EnumUtil enumUtil;

    .
    .
    .


    billingAddress.setStateProvinceCode(enumUtil.getStateCodeFromRegionCode(billingAddressData.getRegion().getIsocode()));
billingAddress.setCountryCode(enumUtil.getCountryCodeFromCountryIso(billingAddressData.getCountry().getIsocode()));

关于这个问题的任何想法?我只想将这两个预先填充的地图注入到类中,并且不确定转换错误来自何处。谢谢!

1 个答案:

答案 0 :(得分:2)

我想你可以尝试这个选项。但它会很冗长。

<property name="countryCodes">
        <util:map map-class="java.util.HashMap" key-type="java.lang.String" value-type="com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.CountryCode">
            <entry>
                <key>US</key>
                <value>
                    <bean class="com.gsk.platform.payment3dsi.cardmanagement.CreditCardManagementServiceStub.CountryCode">
                        <!-- Instantiate your country here --!>
                    </bean>
                </value>
            </entry>
            .....
        </util:map>
    </property>