Spring map注入

test.Configurations定义如下

@Getter
@Setter
public class Configurations {

    private  Map<AnswerSourceType , AbstractWrapper> answerWrappers ;

}

其中,key的类型为枚举类型

public enum AnswerSourceType {

    RERANK("rerank"),

    GUIDANCE("guidance"),


    private String type;

    AnswerSourceType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

在spring配置文件中,通过如下Spring配置注入集合的answerWrappers 变量,其中key只需要配置枚举名称,spring通过类型识别自动状态对应枚举,value则是在spring中注册过的AbstractWrapper实例bean

    <bean class="test.Configurations">
        <property name="answerWrappers">
            <map>
                <entry key="GUIDANCE" value-ref="guidanceWrapper" />
                <entry key="RERANK" value-ref="answerWrapper" />
            </map>
        </property>
    </bean>
原文地址:https://www.cnblogs.com/lmsthoughts/p/7792368.html